Key Task
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.
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.
as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.
1 10
*........X 1 3
*#X 3 20
####################
#XY.gBr.*.Rb.G.GG.y#
#################### 0 0
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的更多相关文章
- hdu 1885 Key Task
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...
- UVALive 3956 Key Task (bfs+状态压缩)
Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...
- HDU 1885 Key Task 国家压缩+搜索
点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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(bfs+状态压缩)
Problem Description The Czech Technical University years of its existence . Some of the university b ...
- HDU1885 Key Task
The Czech Technical University is rather old — you already know that it celebrates 300 years of its ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- hdu 1885 Key Task(bfs+位运算)
题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...
- hdu 1885 Key Task (三维bfs)
题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候 有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...
随机推荐
- 用Apache Kafka构建流数据平台的建议
在<流数据平台构建实战指南>第一部分中,Confluent联合创始人Jay Kreps介绍了如何构建一个公司范围的实时流数据中心.InfoQ前期对此进行过报道.本文是根据第二部分整理而成. ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- HDU 5749 Colmerauer 单调队列+暴力贡献
BestCoder Round #84 1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...
- 【原】Mongodb相关资料
Mongodb与关系型数据库对比 Mongodb与关系型数据库对比 由于之前主要接触的是关系型数据库,所以主要将Mongodb与关系型数据库进行对比:主要从术语.Server与Client.数据定义语 ...
- [算法] 插入排序 Insertion Sort
插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,通常采用in-pla ...
- <Chapter 2>2-2-1.用户偏好模式(The User Preferences Pattern)
在这个模块中我们要创建的应用是一个简单的钟.当一个用户访问这个网站时,这个应用会更具服务器的系统时间显示当前的时间.默认情况下,这个应用使用标准国际时间(UTC)时区显示当前时间.用户可以使用Goog ...
- 【bzoj3233】【ahoi2013】找硬币
题意: 求确定n种货币面额x1..xn满足 x1=1 且xi为xj的整数倍(i>j) 给定n个物品价格ai 求使用上面货币最少需要硬币数(不能找零) 题解: 动态规划 听说网上的题解都是搜索的做 ...
- http 名词解释
get.post.put.delete的安全性和幂等性 安全性:指的是对资料是否有破坏性的操作 幂等性:指的是对资源操作时,数据是一致性.
- java的math常用方法
鉴于java求整时欲生欲死,整理常用math如下: 1: java取整 a:floor向下取整 用法:Math.floor(num) Math.floor(1.9)//1 ...
- (转)UML序列图总结
序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色用一条虚线 ...