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. Linux软件包管理和磁盘管理实践

    一.自建yum仓库,分别为网络源和本地源 本地yum仓库的搭建就是以下三个步骤: 创建仓库目录结构 上传相应的包到目录下,或者直接挂载光盘也行,如果挂载光盘,第三步就可以省略,因为光盘默认里有repo ...

  2. 4、Vim编辑器与正则表达式-面试题

    题目 自己写答案

  3. java 深拷贝与浅拷贝

    yls 2019年11月07日 拷贝分为引用拷贝和对象拷贝 深拷贝和浅拷贝都属于对象拷贝 浅拷贝:通过Object默认的clone方法实现 实现Cloneable接口 public class She ...

  4. 关于html与css的标签及属性(text文本属性、背景background属性、表格标签table、列表、)

    text文本属性1.颜色 colorcolor:red: 2.文本缩进text-indant属性值 num+px text-indant:10px:3.文本修饰 text-decoration属性值: ...

  5. libdispatch.dylib中dispatch_group的实现

    semaphore和group都是libdispatch提供的基于信号量的同步机制,dispatch_group继承自dispatch_semaphore,使用libdispatch层的信号量算法.d ...

  6. WPF 修改屏幕DPI,会触发控件重新加载Unload/Load

    修改屏幕DPI,会触发控件的Unloaded/Loaded 现象/重现案例 对Unloaded/Loaded的印象: FrameworkElement, 第一次加载显示时,会触发Loaded.元素被释 ...

  7. MySQL8.0 新特性 Hash Join

    概述&背景 MySQL一直被人诟病没有实现HashJoin,最新发布的8.0.18已经带上了这个功能,令人欣喜.有时候在想,MySQL为什么一直不支持HashJoin呢?我想可能是因为MySQ ...

  8. JavaScript笔记七

    1.函数 - 返回值,就是函数执行的结果. - 使用return 来设置函数的返回值. - 语法:return 值; - 该值就会成为函数的返回值,可以通过一个变量来接收返回值 - return后边的 ...

  9. python变量、输入输出-xdd

    1.注释 #输入身高,计算BMI 注释1,单行注释... 注释2,多行注释xiedong.. 2.中文编码声明,UTF-8编码声明 # coding=编码 # coding=utf-8 3.建议每行不 ...

  10. linux字符集修改

    首先介绍一下变量. 1.变量类型:本地变量.环境变量.局部变量.特殊变量(内置).参数变量.只读变量. 2.bash的配置文件:profile类和bashrc类 profile类:为交互式登录的she ...