Problem Description
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.

 
Input
The 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: [center][img]../../../data/images/C106-1004-1.JPG[/img][/center]
Note that it is allowed to have [li] more than one exit, [/li] [li] no exit at all, [/li] [li] more doors and/or keys of the same color, and [/li] [li] keys without corresponding doors and vice versa. [/li] 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
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.
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, m, t;
char map[100][100];
bool f[1029][100][100];
int dir[] = {0, 1, 0, -1, 0};
int sum[100];
struct node {
int x, y;
int temp;
__int64 w;
};
int bfs(node x)
{
queue<node>q;
q.push(x);
node a, b;
while (!q.empty()) {
a = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
b = a;
b.x += dir[i];
b.y += dir[i + 1];
if (b.x < 0 || b.x >= n || b.y < 0 || b.y >= m)continue;
if (map[b.x][b.y] == '#')continue;
// if (b.temp >= t)return -1;
if (map[b.x][b.y] == 'b' || map[b.x][b.y] =='y' || map[b.x][b.y] == 'r' || map[b.x][b.y] == 'g') {
b.w|=(1<<(sum[map[b.x][b.y] - 'a']));
}
if (map[b.x][b.y] == 'B' || map[b.x][b.y] =='Y' || map[b.x][b.y] == 'R' || map[b.x][b.y] == 'G') {
if (!(b.w & (1<<sum[map[b.x][b.y] - 'A'])))continue;
}
if (f[b.w][b.x][b.y] != 0)continue;
b.temp++;
f[b.w][b.x][b.y] = true; if (map[b.x][b.y] == 'X') { // if (b.temp >= t)return -1;
return b.temp;
}
q.push(b);
}
}
return -1;
}
int main()
{
node a;
sum['B'-'A']=0,sum['Y'-'A']=1,sum['R'-'A']=2,sum['G'-'A']=3;
while (cin >> n >> m) {
if(n==m&&m==0)break;
memset(f, 0 , sizeof(f));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> map[i][j];
if (map[i][j] == '*') {
a.x = i;
a.y = j;
a.w = 0;
a.temp = 0;
}
}
int c = bfs(a);
if (c != -1)
cout << "Escape possible in " << c << " steps." << endl;
else
cout << "The poor student is trapped!" << endl;
//cout << bfs(a) << endl;
}
return 0;
}

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. hdu 1885 Key Task(bfs+状态压缩)

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

  6. HDU1885 Key Task

    The Czech Technical University is rather old — you already know that it celebrates 300 years of its ...

  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-LINUX试题

    ylbtech-doc:linux-LINUX试题 LINUX试题 1.A,LINUX试题返回顶部 01.{Linux题目}在使用匿名登录ftp时,用户名为(  )? (选择1项) A) login ...

  2. [原创]cocos2d-x + Lua接入iOS原生SDK的实现方案

    相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...

  3. Python 实例: 备份文件

    都说生命苦短,我用python, 所以这两天我也开始学python了. 昨天搞了下语法,今天搞出来个实例,备份文件.尽管编码相当烂,但是测试了一下,还真能用. 它读取一个任务文件, 根据指定的任务参数 ...

  4. Python多线程和Python的锁

    Python多线程 Python中实现多线程有两种方式,一种基于_thread模块(在Python2.x版本中为thread模块,没有下划线)的start_new_thread()函数,另一种基于th ...

  5. C#选择排序详解

    选择排序图解                         选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的 ...

  6. PackageManager获取版本号

    PackageInfo代表的是关于一个包的所有信息,就相当于一个APP应用的清单文件中收集到的所有信息. 通过这个类我们就可以获取类似版本号等一些信息. /** * 得到应用程序的版本名称 */ pr ...

  7. Android相关图书推荐

    疯狂Android讲义(第3版 附光盘) 作      者 李刚 著 出 版 社 电子工业出版社 出版时间 2015-06-01 版      次 3 页      数 780 印刷时间 2015-0 ...

  8. PHP获取Cookie模拟登录

    关键字:CURL Cookie CURLOPT_COOKIEJAR CURLOPT_COOKIEFILE 模拟登录 PHP作者:方倍工作室原文:http://www.cnblogs.com/txw19 ...

  9. 使用Google Chart API绘制组合图

    Google Chart API 绘图 组合图作者:方倍工作室 地址: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN& ...

  10. HDU5727 Necklace

    http://acm.hdu.edu.cn/showproblem.php?pid=5727 题意:n个珠子,每个珠子有阴阳两种属性,且阴的一定和阳的紧邻,排成一个环:m行,每行两个数,表示阳性x珠子 ...