The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places.

The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game.

The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color.

You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.

InputThe input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:

Note that it is allowed to have

  • more than one exit,
  • no exit at all,
  • more doors and/or keys of the same color, and
  • keys without corresponding doors and vice versa.

You may assume that the marker of your position (“*”) will appear exactly once in every map.

There is one blank line after each map. The input is terminated by two zeros in place of the map size.OutputFor each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead.

One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.Sample Input

1 10
*........X 1 3
*#X 3 20
####################
#XY.gBr.*.Rb.G.GG.y#
#################### 0 0

Sample Output

Escape possible in 9 steps.
The poor student is trapped!
Escape possible in 45 steps. 题解:BFS+状态压缩;我们可以记录每一种状态是否出现过来搜索,因为只有4种钥匙,故用二进制表示每一种钥匙的状态;
然后就是普通的BFS,在处理时加上钥匙的处理,以及加判是否为门,如果为门的话,判断上个位置时是否已有该门对应的钥匙,
如有,则该点入队,否则跳过;如果遇到出口,就输出步数,如到最后队列为空时都没有出去,则输出被困在里面;
参考代码为:
 #include<bits/stdc++.h>
using namespace std;
const int N=;
int n,m,sx,sy,ex,ey;
char g[N][N];
bool vis[N][N][];
int dir[][]={{,},{-,},{,},{,-}};
struct node{
int x,y,key,step;
};
int judged(char ch)
{
if(ch=='B')return <<;
if(ch=='Y')return <<;
if(ch=='R')return <<;
if(ch=='G')return <<;
}
int judgek(char ch)
{
if(ch=='b')return <<;
if(ch=='y')return <<;
if(ch=='r')return <<;
if(ch=='g')return <<;
return ;
}
void solve()
{
memset(vis,,sizeof(vis));
queue<node>q;
node u,v;
u.x=sx;u.y=sy;u.key=;u.step=;vis[u.x][u.y][u.key]=;
q.push(u);
while(!q.empty())
{
u=q.front(); q.pop();
if(g[u.x][u.y]=='X')
{
printf("Escape possible in %d steps.\n",u.step);
return;
}
for(int i=;i<;i++)
{
v.x=u.x+dir[i][];v.y=u.y+dir[i][];v.step=u.step+;v.key=u.key|judgek(g[v.x][v.y]);
if(v.x<||v.x>n || v.y<||v.y>m)continue;
if(g[v.x][v.y]=='#' || vis[v.x][v.y][v.key])continue;
if(g[v.x][v.y]=='B'||g[v.x][v.y]=='Y'||g[v.x][v.y]=='R'||g[v.x][v.y]=='G')
{
if(!(u.key&judged(g[v.x][v.y]))) continue;
}
vis[v.x][v.y][v.key]=;
q.push(v);
}
}
printf("The poor student is trapped!\n");
}
int main()
{
while(scanf("%d%d",&n,&m)&&n+m)
{
for(int i=;i<=n;i++) scanf("%s",g[i]+);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(g[i][j]=='*')
{
sx=i;sy=j;g[sx][sy]='.';
break; break;
}
}
}
solve();
}
return ;
}

  

HDU1885 Key Task的更多相关文章

  1. hdu 1885 Key Task

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...

  2. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...

  3. HDU 1885 Key Task 国家压缩+搜索

    点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Time Limit: 3000/1000 MS (Java/Others)  ...

  5. Key Task

    Problem Description The Czech Technical University is rather old - you already know that it celebrat ...

  6. hdu 1885 Key Task(bfs+状态压缩)

    Problem Description The Czech Technical University years of its existence . Some of the university b ...

  7. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

  8. hdu 1885 Key Task(bfs+位运算)

    题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...

  9. hdu 1885 Key Task (三维bfs)

    题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候  有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...

随机推荐

  1. 使用 vue-element-admin 动态路由渲染

    附上:vue-element-admin 官方文档 vue-element-admin https://panjiachen.github.io/vue-element-admin-site/zh/g ...

  2. Maven系列第6篇:生命周期和插件详解,此篇看过之后在maven的理解上可以超越同级别90%的人!

    maven系列目标:从入门开始开始掌握一个高级开发所需要的maven技能. 这是maven系列第6篇. 整个maven系列的内容前后是有依赖的,如果之前没有接触过maven,建议从第一篇看起,本文尾部 ...

  3. pdf2eps implement

    Well, I used the command pdftops in the LaTeX distribution such as MiKTeX/TeXLive/CTex to implement ...

  4. MySql——使用where子句过滤数据

    示例使用的数据表在上一个博客中创建的https://www.cnblogs.com/lbhym/p/11895968.html 参考资料:<Mysql必知必会> 1.使用where子句 示 ...

  5. vue根据不同环境进行编译打包

    工作中我们在开发过程中,有很多的开发环境,如果我们不进行统一配置,那么我们只能手动进行更改,这样会给我们带来诸多不便,所以我们要配置根据不同的环境来进行编译打包. 先看一下我的项目目录: 在confi ...

  6. nyoj 217-a letter and a number (char)

    217-a letter and a number 内存限制:64MB 时间限制:3000ms 特判: No 通过数:4 提交数:5 难度:1 题目描述: we define f(A) = 1, f( ...

  7. ZeroC ICE的远程调用框架 Callback(一)-AMI异步方法调用框架

    Ice框架提供了不少回调设施,其中一些是使用Ice远程调用进行ami模式或amd模式的支撑.本篇来看一下用于代理端的回调设施. Ice代码中有好几个Callback相关命名的基类,并且slice还会为 ...

  8. GitHub远程库的搭建以及使用

    GitHub远程库的搭建 一).配置SSH 步骤: 1).注册GitHub账号 2).本地git仓库与远程的GitHub仓库的传输要通过SSH进行加密 3).创建SSH key ​ 1.检查在用户主目 ...

  9. eNSP仿真软件之利用单臂路由实现VLAN间路由

    1. 实验原理 以太网中,通常会使用VLAN技术隔离二层广播域来减少广播的影响,并增强网络的安全性和可管理性.其缺点是同时也严格地隔离了不同VLAN之间的任何二层流量,使分属于不同VLAN的用户不能直 ...

  10. day 17 re模块 正则表达式

    import re    引用re模块 查找 finall:匹配所有,每一项都是列表中的一个元素 search:只匹配从左到右的第一个,得到的不是直接的结果而是一个变量,通过group方法获取结果,没 ...