Problem Description
The Czech Technical University is rather old — you already know that it celebrates  years of its existence in . 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 -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.
 
Input
The input consists of several maps. Each map begins with a line containing two integer numbers R and C ( ≤ R, C ≤ ) 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.
 
Output
For 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
*........X

*#X

####################
#XY.gBr.*.Rb.G.GG.y#
####################
 
Sample Output
Escape possible in  steps.
The poor student is trapped!
Escape possible in steps.
 
Source
 
 
我的标准写法:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define MAXN 110
struct Node{
int x,y,step;
int key;
};
char map[MAXN][MAXN];
bool mark[MAXN][MAXN][];
int dir[][]={{-,},{,},{,-},{,}};
int n,m;
Node st;
char Door[]={'B','Y','R','G'};
char Key[]={'b','y','r','g'}; void bfs(){
memset(mark,false,sizeof(mark));
queue<Node>Q;
Node p,q;
st.key=st.step=;
mark[st.x][st.y][st.key]=true;
Q.push(st);
while(!Q.empty()){
p=Q.front();
Q.pop();
if(map[p.x][p.y]=='X'){
printf("Escape possible in %d steps.\n",p.step);
return ;
}
for(int i=;i<;i++){
q.x=p.x+dir[i][];
q.y=p.y+dir[i][];
q.step=p.step+;
q.key=p.key;
if(mark[q.x][q.y][q.key]) continue;
if(q.x<||q.x>n||q.y<||q.y>m||map[q.x][q.y]=='#')
continue;
if(isupper(map[q.x][q.y])&&map[q.x][q.y]!='X'){
for(int j=;j<;j++){
if(map[q.x][q.y]==Door[j]){
if(q.key&(<<j)){ mark[q.x][q.y][q.key]=true;
Q.push(q);
}
}
}
}else if(islower(map[q.x][q.y])){
for(int j=;j<;j++){
if(map[q.x][q.y]==Key[j]){
if((q.key&(<<j))==){
q.key+=(<<j);
mark[q.x][q.y][q.key]=true;
Q.push(q);
}
else{
if(mark[q.x][q.y][q.key]==){
mark[q.x][q.y][q.key]=true;
Q.push(q);
}
}
}
}
}else {
mark[q.x][q.y][q.key]=true;
Q.push(q);
}
}
}
puts("The poor student is trapped!");
} int main(){
while(scanf("%d%d",&n,&m),(n+m)){
for(int i=;i<=n;i++){
scanf("%s",map[i]+);
for(int j=;j<=m;j++){
if(map[i][j]=='*')st.x=i,st.y=j;
}
}
bfs();
}
return ;
}

hdu 1885 Key Task(bfs+状态压缩)的更多相关文章

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

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

  2. hdu 1885 Key Task(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1885 再贴一个链接http://blog.csdn.net/u013081425/article/details ...

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

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

  4. hdu 1885 Key Task

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

  5. HDU 1885 Key Task(三维BFS)

    题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...

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

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

  7. hdu 4634 Swipe Bo bfs+状态压缩

    题目链接 状态压缩记录当前拿到了哪些钥匙, 然后暴力搜索. 搞了好几个小时, 一开始也不知道哪里错了, 最后A了也不知道一开始哪里有问题. #include <iostream> #inc ...

  8. hdu 1885 Key Task (三维bfs)

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

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

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

随机推荐

  1. python使用一个集合代替列表

    """说明:对于一个指定的序列,如果需要获得一个只包含该序列中不重复的序列时,使用以下算法:"""seq=['a','a','b','c', ...

  2. [转]用Node.js创建自签名的HTTPS服务器

    用Node.js创建自签名的HTTPS服务器 创建自己的CA机构 创建服务器端证书 创建客户端证书 将证书打包 创建自己的CA机构 为CA生成私钥 openssl genrsa -out ca-key ...

  3. OCP-1Z0-051-题目解析-第28题

    28. Which two statements are true regarding constraints? (Choose two.)  A. A foreign key cannot cont ...

  4. 辛星解读mysql的用户管理

    可能做开发的多半不太关注这方面,可是要说到做运维.那就不能不关注了.由于我们都知道,root的权限太大了.不是随便能用的.我们平时最好用一些比較低的权限的用户.这样会让我们的安全性大大提高,也能防止我 ...

  5. 利用JS实现简单的瀑布流效果

    哈哈, 我又来啦, 在这一段时间里, 我简单的学习了一下javascript(JS), 虽然不是很懂啦, 但是我也简单的尝试着做了点小东西, 就比如现在流行的瀑布流效果, 经过我的努力终于成功的完成了 ...

  6. 查看linux版本和内核信息

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@localhost ~]# cat /proc/versionLinux version 2.6.32 ...

  7. for循环和经典案例

    循环:初始条件,循环条件,状态改变,循环体.for(初始条件;循环条件;状态改变){ 循环体}for(int i=1;i<=10;i++){ }例子:100以内与7有关的数.求100以内所有数的 ...

  8. 3D图片采集与展示(SurfaceView 自适应 Camera, 录制视频, 抽取帧)

    最近在做一个3D图片采集与展示. 主要功能为:自定义Camera(google 已经摈弃了Camera, 推荐使用Camera2,后续篇幅,我将会用Camera2取代Camera),围绕一个物体360 ...

  9. (五)CodeMirror - 关于htmlmixed中包含script脚本

    最近发现个问题,场景如下: 当创建的mode类型为htmlmixed,且内容中包含javascript脚本,且是闭包立即执行: 如果内容是使用JQuery函数.html()插入到DOM中后再创建cod ...

  10. JVM的内存区域划分划分及作用