hdoj 1429 胜利大逃亡(续)【bfs好题】
2016-04-04 16:43
239 查看
胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7340 Accepted Submission(s): 2544
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
Source
ACM暑期集训队练习赛(三)
Recommend
linle
Statistic | Submit | Discuss | Note
代码:
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define INF 0x3f3f3f3f using namespace std; int n,m,t; char mp[25][25]; int vis[25][25][1<<11];//a,b,c,d,e,f,g,h,i,j int sx,sy,ex,ey; int ans; int dx[4]={0,1,-1,0}; int dy[4]={1,0,0,-1}; struct node { int x,y,step,key; }a,temp; int judge() { if(temp.x<0||temp.x>=n) return 0; if(temp.y<0||temp.y>=m) return 0; if(temp.step>t) return 0; if(mp[temp.x][temp.y]=='*') return 0; return 1; } void bfs() { queue<node>q; a.x=sx;a.y=sy;a.step=0,a.key=0; q.push(a); memset(vis,0,sizeof(vis)); vis[sx][sy][0]=1; while(!q.empty()) { a=q.front(); q.pop(); for(int i=0;i<4;i++) { temp.x=a.x+dx[i]; temp.y=a.y+dy[i]; temp.step=a.step+1; if(judge()) { if(mp[temp.x][temp.y]>='a'&&mp[temp.x][temp.y]<='j') { temp.key=a.key|((1<<(mp[temp.x][temp.y]-'a'))); if(!vis[temp.x][temp.y][temp.key]) { vis[temp.x][temp.y][temp.key]=1; q.push(temp); } } else if(mp[temp.x][temp.y]>='A'&&mp[temp.x][temp.y]<='J') { temp.key=a.key; if(temp.key&(1<<(mp[temp.x][temp.y]-'A'))) { if(!vis[temp.x][temp.y][temp.key]) { vis[temp.x][temp.y][temp.key]=1; q.push(temp); } } } else { temp.key=a.key; if(!vis[temp.x][temp.y][temp.key]) { if(temp.x==ex&&temp.y==ey) { ans=temp.step; return; } vis[temp.x][temp.y][temp.key]=1; q.push(temp); } } } } } } int main() { while(scanf("%d%d%d",&n,&m,&t)!=EOF) { ans=INF; for(int i=0;i<n;i++) { scanf("%s",mp[i]); for(int j=0;j<m;j++) { if(mp[i][j]=='@') { sx=i;sy=j; } else if(mp[i][j]=='^') { ex=i,ey=j; } } } bfs(); if(ans<t) printf("%d\n",ans); else printf("-1\n"); } return 0; }
相关文章推荐
- hdoj 第十一页 Ratio(Accepted/Submissions)排序
- iOS 简单动画 序列帧动画
- 10秒后跳转
- POJ 1753 Flip Game(搜索+位运算)
- 【April Fools Day Contest 2016B】【暴力】Scrambled 至少满足其一 模a余b
- gmtime和ctime的区别(补)
- CPU和显卡搭配
- 自定义九宫格手势解锁
- 【April Fools Day Contest 2016A】【OEIS】Da Vinci Powers 达芬奇power
- 【一天一道LeetCode】#8. String to Integer (atoi)
- 【一天一道LeetCode】#8. String to Integer (atoi)
- 项目需求分析
- mySQL
- Linux特殊权限之s、t、i、a
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
- ds
- 自定义的ArrayList
- 约瑟夫环【dp】
- CSP考试 2013年12月第4题 C语言实现
- 背包问题