HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个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 + 状态压缩)的更多相关文章
- hdu 1885 Key Task(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1885 再贴一个链接http://blog.csdn.net/u013081425/article/details ...
- 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
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...
- HDU 1885 Key Task(三维BFS)
题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...
- HDU 1885 Key Task 国家压缩+搜索
点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 4634 Swipe Bo bfs+状态压缩
题目链接 状态压缩记录当前拿到了哪些钥匙, 然后暴力搜索. 搞了好几个小时, 一开始也不知道哪里错了, 最后A了也不知道一开始哪里有问题. #include <iostream> #inc ...
- hdu 1885 Key Task(bfs+状态压缩)
Problem Description The Czech Technical University years of its existence . Some of the university b ...
- hdu 1885 Key Task (三维bfs)
题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候 有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...
- hdu 1885 Key Task(bfs+位运算)
题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...
随机推荐
- windows Server 2008 IE增强的安全配置关闭方法
解决方法 开始->管理工具->服务器管理器
- 创建线程方式-GCD
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- EL简介
一.EL简介 1.语法结构 ${expression}2.[]与.运算符 EL 提供.和[]两种运算符来存取数据. 当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符 ...
- Linux网络状态工具ss命令使用详解
ss命令用于显示socket状态. 他可以显示PACKET sockets, TCP sockets, UDP sockets, DCCP sockets, RAW sockets, Unix dom ...
- delphi Inc函数和Dec函数的用法
inc自增函数 .inc(i,n)://i,n:integer;n为自增量 相当于i:=i+n: .inc(i)://i:integer; 相当于i:=i+; dec自减函数 .dec(i,n): ...
- ArcGIS API for Flex实现GraphicsLayer上画点、线、面。
目的: ArcGIS API for Flex实现GraphicsLayer上画点.线.面. 准备工作: 1.这次地图数据就用Esri提供的http://server.arcgisonline.com ...
- spring集成freemaker 制作短信模板
1.配置configure的Bean,Bean中指定了模板文件的路径和刷新时间等配置. <!-- 配置freeMarkerConfigurer进行属性值的注入 --> <bean i ...
- NLPP-03-Exercises
<PYTHON自然语言处理>第3章 处理原始文本 更多更复杂有关处理HTML内容 http://www.crummy.com/software/BeautifulSoup/ 3.11 深入 ...
- 运行时报错-Verify the Developer App certificate for youraccount is trusted on your device. Open Settings on Mayoyi_sakura and navigate to General -> Device Management, then select your
解决方法:打开手机设置->通用->设备管理,找到编辑工程时的ID资料,点击允许即可.
- Redis 软件和配置
Redis 下载 1.通过CMD命令进入redis 文件目录 2.运行[redis-server redis.windows.conf]