hdu 1885 Key Task(bfs+位运算)
题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母)。问能否完成,若能,花费的时间是多少。
分析:同hdu 1429,只不过这里用map把四种钥匙标号了,否则爆内存。
错误:判断门的条件用 isupper(ch) 表示,所以终点'X'也在这个范围内。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<map>
#include<queue>
#include<algorithm>
using namespace std; const int MAXN=;
const int KEY=; map<char,int> mp; int dir[][]={,-,,,-,,,}; struct Node{
int x,y,t,key;
Node(){}
Node(int _x,int _y,int _t,int _key):x(_x),y(_y),t(_t),key(_key){}
}; char g[MAXN][MAXN];
int mark[MAXN][MAXN][<<KEY];
queue<Node>q; int n,m,T; bool wall(int x,int y)
{
if(x<||x>=n||y<||y>=m)
return true;
if(g[x][y]=='#')
return true;
return false;
} int bfs(int sx,int sy)
{
while(!q.empty())
q.pop(); memset(mark,,sizeof(mark));
mark[sx][sy][]=;
q.push(Node(sx,sy,,));
while(!q.empty())
{
Node e=q.front();q.pop(); for(int i=;i<;i++)
{
int dx=e.x+dir[i][];
int dy=e.y+dir[i][];
int dt=e.t+;
int dkey=e.key; char ch=g[dx][dy]; if(wall(dx,dy))
continue;
if(mark[dx][dy][dkey])
continue;
if(g[dx][dy]=='X')
return dt;
if(isupper(ch)&&!(dkey&(<<mp[ch])))
continue;
if(islower(ch))
dkey=dkey|(<<mp[ch]); mark[dx][dy][dkey]=;
q.push(Node(dx,dy,dt,dkey));
}
}
return -;
} int main()
{
mp['B']=mp['b']=;
mp['Y']=mp['y']=;
mp['R']=mp['r']=;
mp['G']=mp['g']=;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m)
return ; for(int i=;i<n;i++)
scanf("%s",g[i]); int sx,sy,ex,ey;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(g[i][j]=='*'){
sx=i;
sy=j;
g[i][j]='.';
} int ans=bfs(sx,sy);
if(ans==-)
printf("The poor student is trapped!\n");
else
printf("Escape possible in %d steps.\n",ans);
}
return ;
}
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 + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- 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 1885 Key Task (三维bfs)
题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候 有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...
- hdu 1885 Key Task(bfs+状态压缩)
Problem Description The Czech Technical University years of its existence . Some of the university b ...
- HDU5627--Clarke and MST (bfs+位运算)
http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...
随机推荐
- Using an Interface as a Type
When you define a new interface, you are defining a new reference data type. You can use interface n ...
- 动态调用wcf接口服务
1.url:http://localhost:8002/名称.svc/basic(.svc结尾) 2.需要引用的命名空间System.ServiceModel 3.调用代码: public class ...
- POJ 1523 SPF(求割点)
题目链接 题意 : 找出图中所有的割点,然后输出删掉他们之后还剩多少个连通分量. 思路 : v与u邻接,要么v是u的孩子,要么u是v的祖先,(u,v)构成一条回边. //poj1523 #includ ...
- hdu 1404/zoj 2725 Digital Deletions 博弈论
暴力打表!! 代码如下: #include<iostream> #include<algorithm> #include<cstdio> #include<c ...
- android-non-ui-ui-thread-communications-part-5-5
This is the last post in my series regarding Android thread communications. Parts 1 through 4 are l ...
- Struts2 中的值栈的理解
通过对struts2的一段时间的接触,将自己对OGNL的核心值栈说说,值栈:简单的说,就是存放action的堆栈,当我们提交一个请求 道服务器端 action时,就有个堆栈,如果action在服务器端 ...
- How to say "no"?
How to say "no"?7招教你如何拒绝别人 Do you have a hard time saying no to others? Do you say “y ...
- eclipse导入maven项目后依赖jar包更新问题->update project按钮
eclipse导入maven项目后依赖jar包更新问题 1.eclipse有专门的导入maven项目按钮,file-import-maven project,eclipse会自动查找指定路径下的pom ...
- 自绘CProgressCtrl进度条控件,支持自定义显示文本和进程百分比信息
// CXProgressCtrl 头文件 #pragma once // CXProgressCtrl class CXProgressCtrl : public CProgressCtrl { D ...
- Golang哲学思想
Golang是一门新语言,经过几年发展,慢慢地也已经被许多大公司认可.最大的特点是速度快,并发性好,与网络的功能结合好,是一门服务端语言,号称“网络时代的新语言”:另外还是一个编译型的Python.不 ...