B - 胜利大逃亡(续) HDU - 1429 bfs
2017-05-25 21:32
351 查看
胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8642 Accepted Submission(s): 3117
[align=left]Problem Description[/align]
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……
这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
[align=left]Input[/align]
每组测试数据的第一行有三个整数n,m,t(2<=n,m<=20,t>0)。接下来的n行m列为地牢的地图,其中包括:
. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J
每组测试数据之间有一个空行。
[align=left]Output[/align]
针对每组测试数据,如果可以成功逃亡,请输出需要多少分钟才能离开,如果不能则输出-1。
[align=left]Sample Input[/align]
4 5 17
@A.B.
a*.*.
*..*^
c..b*
4 5 16
@A.B.
a*.*.
*..*^
c..b*
[align=left]Sample Output[/align]
16
-1
[align=left]Author[/align]
LL
bfs,用了一个分层图的概念,其实就是把拿到了那些钥匙这个状态给压缩起来,这样同一点在每一个种状态下只能走一次,
#include<bits/stdc++.h> using namespace std; char mapp[30][30]; int book[1030][30][30]; struct node { int now,step,x,y; }; int n,m,T; int check(node t) { if(t.x<0||t.x>=n||t.y<0||t.y>=m||mapp[t.x][t.y]=='*') return 1; return 0; } int ne[4][2]={-1,0,1,0,0,1,0,-1}; int bfs(node beginn) { memset(book,0,sizeof(book)); queue<node>q; book[0][beginn.x][beginn.y]=1; q.push(beginn); node re,pe; while(!q.empty()) { pe=q.front(); q.pop(); if(mapp[pe.x][pe.y]=='^') { if(pe.step+1<=T) { return pe.step; } else return -1; } if(pe.step>T) return -1; for(int i=0;i<4;i++) { re=pe; re.x+=ne[i][0]; re.y+=ne[i][1]; if(check(re)) continue; if(mapp[re.x][re.y]<='J'&&mapp[re.x][re.y]>='A') { int tt=mapp[re.x][re.y]-'A'; if(((1<<tt)&re.now)==0) { continue; } } else if(mapp[re.x][re.y]<='j'&&mapp[re.x][re.y]>='a') { int tt=mapp[re.x][re.y]-'a'; re.now|=(1<<tt); } if(book[re.now][re.x][re.y]) continue; re.step++; book[re.now][re.x][re.y]=1; q.push(re); } } return -1; } int main() { int i,j; while(cin>>n>>m>>T) { for(i=0;i<n;i++) { scanf("%s",mapp[i]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(mapp[i][j]=='@') { node tt; tt.x=i; tt.y=j; tt.step=0; tt.now=0; printf("%d\n",bfs(tt)); } } } } return 0; }
相关文章推荐
- HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
- HDU 1429 胜利大逃亡(续) (BFS+位压缩)
- HDU 1429 胜利大逃亡(续)(状压bfs)
- HDU 1429 胜利大逃亡(续)(BFS+状态压缩)
- HDU 1429 胜利大逃亡(续)(三维BFS)
- hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
- HDU 1429 胜利大逃亡(续) (BFS )
- hdu 1429胜利大逃亡(续) / sdut 2193 救基友记3(BFS)
- HDU-1429:胜利大逃亡(续)(bfs+简单状压)
- hdu 1429 胜利大逃亡(续)(BFS+位压缩)
- HDU 1429 胜利大逃亡(续) / SDUT 2193 救基友记3 简单的BFS
- hdu 1429 胜利大逃亡(续) 二进制bfs
- hdu 1429 胜利大逃亡(续)(BFS+位压缩)
- HDU 1429 胜利大逃亡(续)(状态压缩表示,BFS)
- HDU 1429 胜利大逃亡(续)(bfs)
- [BFS] HDU 1429 胜利大逃亡(续)
- hdu1429 胜利大逃亡(续) (BFS+状态压缩)
- hdu 1429胜利大逃亡(续) (bfs+状态压缩)
- hdu 1429 胜利大逃亡(续)(bfs+状态压缩)
- Hdu-1429-胜利大逃亡(续) [bfs]+[状态压缩]