题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少。

析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态,然后再BFS,就简单了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 1, 0, -1};
const int dc[] = {1, 0, -1, 0};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int x, y, d, state;
Node() { }
Node(int xx, int yy, int dd, int k) : x(xx), y(yy), d(dd), state(k) { }
};
char s[maxn][maxn];
bool vis[maxn][maxn][20];
map<char, int> key, door; void bfs(int x, int y){
queue<Node> q;
memset(vis, 0, sizeof vis);
q.push(Node(x, y, 0, 0));
vis[x][y][0] = true; while(!q.empty()){
Node u = q.front(); q.pop();
for(int i = 0; i < 4; ++i){
int x = u.x + dr[i];
int y = u.y + dc[i];
int d = u.d + 1;
if(!is_in(x, y)) continue;
if(s[x][y] == 'X'){
printf("Escape possible in %d steps.\n", d);
return ;
}
if(s[x][y] == '#') continue;
int state;
if(islower(s[x][y])) state = u.state | (1 << key[s[x][y]]);
else if(isupper(s[x][y])){
if(u.state & (1<<door[s[x][y]])) state = u.state;
else continue;
}
else if(s[x][y] == '.' || s[x][y] == '*') state = u.state;
if(!vis[x][y][state]){
vis[x][y][state] = true;
q.push(Node(x, y, d, state));
}
}
}
printf("The poor student is trapped!\n");
} int main(){
key['b'] = 1; door['B'] = 1;
key['y'] = 2; door['Y'] = 2;
key['r'] = 3; door['R'] = 3;
key['g'] = 0; door['G'] = 0;
while(scanf("%d %d", &n, &m) == 2){
if(!n && !m) break;
for(int i = 0; i < n; ++i) scanf("%s", s+i);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(s[i][j] == '*') { bfs(i, j); break; }
}
return 0;
}

HDU 1885 Key Task (BFS + 状态压缩)的更多相关文章

  1. hdu 1885 Key Task(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1885 再贴一个链接http://blog.csdn.net/u013081425/article/details ...

  2. HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Time Limit: 3000/1000 MS (Java/Others)  ...

  3. hdu 1885 Key Task

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...

  4. HDU 1885 Key Task(三维BFS)

    题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...

  5. HDU 1885 Key Task 国家压缩+搜索

    点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. hdu 4634 Swipe Bo bfs+状态压缩

    题目链接 状态压缩记录当前拿到了哪些钥匙, 然后暴力搜索. 搞了好几个小时, 一开始也不知道哪里错了, 最后A了也不知道一开始哪里有问题. #include <iostream> #inc ...

  7. hdu 1885 Key Task(bfs+状态压缩)

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

  8. hdu 1885 Key Task (三维bfs)

    题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候  有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...

  9. hdu 1885 Key Task(bfs+位运算)

    题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...

随机推荐

  1. Quartz.NET管理周期性任务

    Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了作业和 ...

  2. apache配置Options详解

    http://www.365mini.com/page/apache-options-directive.htm Options指令是Apache配置文件中一个比较常见也比较重要的指令,Options ...

  3. gcc与makefile编译 BY 四喜三顺

    gcc编译控制过程:(假设源代码为a.c)(1)源文件到预处理文件:    gcc -E -o a.cxx a.c    a.cxx显示调用哪些头文件(2)生成汇编代码:              g ...

  4. HDU2047

    http://acm.hdu.edu.cn/showproblem.php?pid=2047 对于这道题,我就从后面向前面考虑. 当第n个是o的话,那么n-1 只可以取e或者f,如果n是e或者f的话, ...

  5. jmeter的逻辑控制器

    这篇是在网上找的,写的实在是比我写的具体得多,也没什么好补充的,拿来记录一下,方便以后查询,感激原作者!! JMeter中的Logic Controller分为两类:一类用来控制Test Plan执行 ...

  6. IQ推理:P先生和Q先生

    P先生.Q先生具有足够的推理能力.这天,他们正在接受面试. 他们知道桌子的抽屉里有16张扑克牌:红桃 A Q 4黑桃 J 8 4 2 7 3草花 K Q 5 4 6方块 A 5约瀚教授从这16张牌中挑 ...

  7. NandFlash读写

    1.NandFlash分类 根据物理结构上的区别,NandFlash主要分为如下两类:•SLC (Single Level Cell): 单层式存储•MLC (Multi Level Cell): 多 ...

  8. 2016-07-07: 重新编译时vc90.pdb不是创建此预编译头时使用的pdb文件

    使用VS2008在一个解决方案中包含多个项目时,当设置多个项目的中间目录为同一个目录时,在增量编译时出现"重新编译时vc90.pdb不是创建此预编译头时使用的pdb文件,请重新创建预编译头问 ...

  9. LC.exe已退出,代码为-1错误

    因为证书的原因,把项目中“properties”目录下的“license.licx”文件删除,再编译就成功了.如图:

  10. nullcon HackIM2016 -- Programming Question 3

    Still Hungry and unsutisfied, you are looking for more. Some more, unique un heard dishes. Then you ...