hdu 1885 Key Task(bfs+状态压缩)
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.
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.
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.
*........X *#X ####################
#XY.gBr.*.Rb.G.GG.y#
####################
Escape possible in steps.
The poor student is trapped!
Escape possible in steps.
#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+状态压缩)的更多相关文章
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- hdu 1885 Key Task(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1885 再贴一个链接http://blog.csdn.net/u013081425/article/details ...
- HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Time Limit: 3000/1000 MS (Java/Others) ...
- hdu 1885 Key Task
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...
- HDU 1885 Key Task(三维BFS)
题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...
- HDU 1885 Key Task 国家压缩+搜索
点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 4634 Swipe Bo bfs+状态压缩
题目链接 状态压缩记录当前拿到了哪些钥匙, 然后暴力搜索. 搞了好几个小时, 一开始也不知道哪里错了, 最后A了也不知道一开始哪里有问题. #include <iostream> #inc ...
- hdu 1885 Key Task (三维bfs)
题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候 有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...
- hdu 1885 Key Task(bfs+位运算)
题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...
随机推荐
- html li标签前面添加图标三种方法
今天无聊写下这个例子,希望对初学者有帮助,代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf ...
- java如何从方法返回多个值
本文介绍三个方法,使java方法返回多个值. 方法1:使用集合类 方法2:使用封装对象 方法3:使用引用传递 示例代码如下: import java.util.HashMap; import java ...
- Sublime text3配置LiveReload
Tip: LiveReload是很棒的插件,可以在浏览器中实时预览,但是在Sublime text3里,从Package Control中安装的LiveReload是无法使用的,但是可以选择手动安装解 ...
- C#获取文件和文件夹大小
代码如下: /// <summary> /// 获取文件夹大小 /// </summary> /// <param name="dirPath"> ...
- C#语法糖: 扩展方法(常用)
今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...
- sql 表名为关键字
user在sql server中时一个关键字,如上面说所的,有时候我们无意中将其作为表的名称,当我们在sql语句中要使用该名称时例如:select *from user这是会提示user附近有语法错误 ...
- asp.net中使用forms验证
1.首先在web.config中修改验证方式为 "Forms" <authentication mode="Forms"> 这里的模式有很多中,可自 ...
- gitosis随记
0.创建git用户 useradd -m git passwd git 1.安装脚本工具(gitosis依赖python) apt-get install python-setuptools 2.gi ...
- IOC设计模式初步了解(day02)
IOC(Inversion of Control):控制反转. *其他解释:依赖注入.依赖反转…… 设计目标:简化JEE的研发工作,提供IOC容器,控制bean的生成.注入,解耦. 看了网上的一些帖子 ...
- js迭代器模式
在迭代器模式中,通常有一个包含某种数据的集合的对象.该数据可能储存在一个复杂数据结构内部,而要提供一种简单 的方法能够访问数据结构中的每个元素. 实现如下: //迭代器模式 var agg = (fu ...