HDU 1429 胜利大逃亡(续)(状态压缩表示,BFS)
2016-08-07 09:51
232 查看
胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7872 Accepted Submission(s): 2789
Problem Description
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……
这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
Input
每组测试数据的第一行有三个整数n,m,t(2<=n,m<=20,t>0)。接下来的n行m列为地牢的地图,其中包括:
. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J
每组测试数据之间有一个空行。
Output
针对每组测试数据,如果可以成功逃亡,请输出需要多少分钟才能离开,如果不能则输出-1。
Sample Input
4 5 17
@A.B.
a*.*.
*..*^
c..b*
4 5 16
@A.B.
a*.*.
*..*^
c..b*
Sample Output
16
-1
Author
LL
题意:中文题不解释
思路:有钥匙才能开对应的门,那么我们就把一共十把钥匙用二进制表示起来,1表示已经有这把钥匙了,0表示没有。那么当遇到门的时候只有对应位为1才能过去,这点我们可以用位运算快速得到。
有些人可能会问标记数组怎么开? 答案是开vis[20][20][1024] 我一开始也以为会炸掉,没想到轻松的过了。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
#define N 21
int n,m,t;
char ma
;
int dir[4][2]= {-1,0,1,0,0,-1,0,1};
bool vis
[1025];
struct Node
{
int x,y;
int num,step;
};
int bfs()
{
queue<Node>que;
Node a,next;
a.num=0;
a.step=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(ma[i][j]=='@')
{
a.x=i,a.y=j;
break;
}
que.push(a);
memset(vis,false,sizeof(vis));
vis[a.x][a.y][a.num]=true;
while(!que.empty())
{
Node now=que.front();
que.pop();
if(now.step>=t) return -1;
if(ma[now.x][now.y]=='^') return now.step;
for(int i=0; i<4; i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
if(next.x<0||next.x>=n||next.y<0||next.y>=m||ma[next.x][next.y]=='*') continue;
next.step=now.step+1;
next.num=now.num;
if(ma[next.x][next.y]>='A'&&ma[next.x][next.y]<='Z')
if(!(next.num&(1<<(ma[next.x][next.y]-'A'))))
continue;
if(ma[next.x][next.y]>='a'&&ma[next.x][next.y]<='z')
next.num=next.num|(1<<(ma[next.x][next.y]-'a'));
if(!vis[next.x][next.y][next.num])
{
vis[next.x][next.y][next.num]=1;
que.push(next);
}
}
}
return -1;
}
int main()
{
while(~scanf("%d %d %d",&n,&m,&t))
{
for(int i=0; i<n; i++)
scanf("%s",ma[i]);
printf("%d\n",bfs());
}
return 0;
}
相关文章推荐
- hdu1429 胜利大逃亡(续) (BFS+状态压缩)
- HDU 1429 胜利大逃亡(续)(BFS+状态压缩)
- hdu 1429胜利大逃亡(续) (bfs+状态压缩)
- hdu 1429 胜利大逃亡(续)(bfs+状态压缩)
- HDU 1429 胜利大逃亡(续) 状态压缩BFS
- HDU 1429 胜利大逃亡(续)(BFS+状态压缩)
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
- HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
- hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
- hdu 1429 胜利大逃亡(续)(状态压缩+bfs)
- Hdu-1429-胜利大逃亡(续) [bfs]+[状态压缩]
- hdu-1429 胜利大逃亡(续) (BFS+状态压缩)
- HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
- hdu 1429 胜利大逃亡(续) 状态压缩+BFS
- HDU 1429 胜利大逃亡(续) (状态压缩+BFS)
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
- HDU 1429 胜利大逃亡(续)(广搜+状态压缩)
- hdu 1429 胜利大逃亡(续) (bfs + 状态压缩)
- HDU-1429 胜利大逃亡(续) (BFS+状态压缩)
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)