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 ...
随机推荐
- eclipse中如何关闭运行时自动保存?
Eclipse没有提供自动保存的功能,只能自己写脚本每隔多久保存一次,或监听修改即保存.设置方法:1.打开:preferences>run/debug>launching2.选择save ...
- Django TemplateView
主要功能是渲染模板,看官例: from django.views.generic.base import TemplateView from articles.models import Articl ...
- cocos2dx3.0 removeFromParent和removeAllChildren含义
顾名思义,removeFromParent就是把自己从父亲那里移除,removeAllChildren就是移除自己所有的孩子,这些方法的具体实现都在基类Node里面,通过查看代码也很容易看到怎么实现的 ...
- Android软件更新安装。
app的开发有一个问题是避免不了的,那就是软件的升级维护. 这里我在查过一些资料和写了一个升级帮助类.使用很方便.直接导入就可以了. ( VersionBean.class为更新地址返回的数据对象,我 ...
- leetcode 169
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 红星美凯龙CEO车建新的圆融和霸气
待人接物中车建新有许多习惯,与别人一起行走时,走在靠马路的一边:吃饭时最好的菜留给客人.他说,做人往往就在细节中,别小看一个举动,无意中就会感染别人.和别人在一起,你要时时刻刻先考虑对方. 细节上体察 ...
- windows下安装多个tomcat服务
摘要 公司服务器已经部署2个tomcat,分别属于不同的系统.今天新开发的系统也要上线测试,故新增一个tomcat服务器. 1.官网下载tomcat 7 解压缩版本.我使用的是 apache-tomc ...
- Redis和Memcached对比
Redis和Memcached对比 这两年 Redis火得可以,Redis也常常被当作 Memcached的挑战者被提到桌面上来.关于Redis与Memcached的比较更是比比皆是.然而,Redis ...
- OpenLDAP与phpldapadmin的搭建
最近一直在看LDAP的东西,把自己的记录下来,以后可以看看. 1:环境 1):关闭防火墙 service iptables stop 2):setenforce 0 vim /etc/sysconfi ...
- [html]选项卡效果
晨间新闻 午间新闻 晚间新闻 视频新闻 <!doctype html> <html> <head> <meta charset="UTF-8&quo ...