HDOJ 1429 胜利大逃亡(续) (BFS + 状态压缩)
2017-04-03 00:53
316 查看
胜利大逃亡(续)
[b]Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8413 Accepted Submission(s): 3019
[/b]
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
#include <bits/stdc++.h>
using namespace std;
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
const int N=25;
struct node
{
int x,y,time;
int key;
};
int vis
[1<<10];
char mp
;
int n,m,t,flag;
node st;
int inbound(node s)
{
return s.x>=0&&s.x<n&&s.y>=0&&s.y<m;
}
void bfs(node st)
{
node cur,nex;
queue<node>q;
vis[st.x][st.y][st.key]=1;
q.push(st);
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur.time<t&&mp[cur.x][cur.y]=='^')
{
printf("%d\n",cur.time);
flag=1;
return;
}
f(i,0,4)
{
nex.x=cur.x+dir[i][0];
nex.y=cur.y+dir[i][1];
nex.time=cur.time+1;
nex.key=cur.key;
if(mp[nex.x][nex.y]>='a'&&mp[nex.x][nex.y]<='j')
nex.key=cur.key|(1<<(mp[nex.x][nex.y]-'a'));
if(mp[nex.x][nex.y]=='*'||nex.time>=t) continue;
else if(inbound(nex)&&!vis[nex.x][nex.y][nex.key])
{
vis[nex.x][nex.y][nex.key]=1;
if(mp[nex.x][nex.y]>='A'&&mp[nex.x][nex.y]<='J')
{
if(nex.key&(1<<(mp[nex.x][nex.y]-'A')))
q.push(nex);
}
else q.push(nex);
}
}
}
return;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&t))
{
flag=0;
mst(vis);
f(i,0,n)
f(j,0,m)
{
scanf(" %c",&mp[i][j]);
if(mp[i][j]=='@')
{
mp[i][j]='.';
st.x=i;
st.y=j;
st.time=0;
st.key=0;
}
}
bfs(st);
if(!flag) printf("-1\n");
}
return 0;
}
相关文章推荐
- hdoj 1429 胜利大逃亡(续) 【BFS + 状态压缩】
- hdoj 1429 胜利大逃亡(续)< BFS + 状态压缩>
- HDOJ 1429 胜利大逃亡(续)(BFS + 状态压缩)
- (HDU 1429)胜利大逃亡(续) BFS + 二进制状态压缩
- HDU 1429 胜利大逃亡(续) (BFS + 状态压缩)
- hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】
- hdu 1429 胜利大逃亡(续) (bfs + 状态压缩)
- hdu 1429 胜利大逃亡(续) (bfs + 状态压缩)
- hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】
- hdu1429 胜利大逃亡(续) (广搜+状态压缩)
- hdu 1429 胜利大逃亡(续)(状态压缩+bfs)
- hdu 胜利大逃亡(续) bfs+状态压缩 1429 钥匙与门相对应
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
- hdu(1429) 胜利大逃亡(续) 队列+bfs+状态压缩
- hdu 1429 胜利大逃亡(续)(bfs+状态压缩)
- HDU ACM 1429 胜利大逃亡(续) (广搜+二进制状态压缩)
- HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
- HDOJ 4771 Stealing Harry Potter's Precious(bfs + 状态压缩)
- hdu 1429胜利大逃亡(续) (bfs+状态压缩)
- HDOJ 5025 Saving Tang Monk(BFS + 优先队列 + 状态压缩)