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. 什么情况用ArrayList or LinkedList呢?

    ArrayList 和 LinkedList 是 Java 集合框架中用来存储对象引用列表的两个类.ArrayList 和 LinkedList 都实现 List 接口.先对List做一个简单的了解: ...

  2. java 深拷贝与浅拷贝

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

  3. Maven系列第8篇:你的maven项目构建太慢了,我实在看不下去,带你一起磨刀!!多数使用maven的人都经常想要的一种功能,但是大多数人都不知道如何使用!!!

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

  4. 开始逆向objc基础准备(一)简单认识一下arm32,以及与x86汇编指令类比

    ARM32体系中有31或33个通用寄存器,没有特定的某种态下有r0-r15一共16个寄存器,快速中断态下有另一组r8-r12备份寄存器,在用户态和系统态之外其它态下都各自有一组r13-r14备份寄存器 ...

  5. js对象的sessionStorage,判断对象相等,判断是否包含某属性

    一,storage storage只能存储字符串的数据,对于JS中常用的数组或对象却不能直接存储 因此需要借JSON进行类型转化来存储: let obj = { name:'Jim' } sessio ...

  6. Django使用mysql数据的流程

    创建一个mysql数据库 1.打开终端(cmd): 输入: mysql -uroot -p 密码:*** 输入: create database 数据库名字; 2.在settings中进行配置 DAT ...

  7. web前端之css基础

    CSS选择器 元素选择器 p{color:red;} ID选择器 #li{ background-color:red; } 类选择器 .c1{ font-size:15px; } 注意: 样式类名不要 ...

  8. 【NHOI2018】衰减

    [解题思路] 显然这题并不难,由于数据范围较小,完全可以用DFS解决. 从原数开始每次变异的图谱,每次记录住当前的路径. 当找到1时就可以输出并回溯了. 小技巧:printf和scanf可以提高输出输 ...

  9. Python 信息提取-爬虫

    import requests import re from bs4 import BeautifulSoup url = "http://python123.io/ws/demo.html ...

  10. Eclipse设置Working Set管理项目和detach合并分离窗口

    当项目多了的时候,使用Working Set分组管理项目很有必要了,不然一大推项目在一起 找起来麻烦,看起来也难受~ ​ 所以根据给项目不同分类就很有必要了. 之前myeclipse设置了,今天装了一 ...