hdu 1242 Rescue(bfs)
2015-08-24 14:57
513 查看
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21802 Accepted Submission(s): 7767
[align=left]Problem Description[/align]
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
[align=left]Input[/align]
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
[align=left]Output[/align]
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
[align=left]Sample Input[/align]
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
[align=left]Sample Output[/align]
13
[align=left]Author[/align]
CHEN, Xue
[align=left]Source[/align]
ZOJ Monthly, October 2003
[align=left]Recommend[/align]
Eddy | We have carefully selected several similar problems for you: 1072 1372 1180 1401 1239
因为有些点需要走两秒,所以路程最短的路不一定是时间最短,所以需要使用优先队列,实际为优先队列的模板题。
题意:有一名天使被囚禁了,天使的朋友要去救他,你的任务是输出最短的救援时间。天使的朋友每秒可以走一步,地牢中有守卫,当你遇到守卫的时候需要停留一秒杀死守卫。给你地牢的地图,上面有几种元素,'.'表示路,可以通行。'#'代表墙,无法通行。'r'表示天使的朋友,代表起点。'a'表示天使的位置,代表终点。'x'表示守卫的位置。
附上代码:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M=205; char map[M][M]; int n,m,visit[M][M]; int f[4][2]= {0,1,0,-1,1,0,-1,0}; //模拟四个方向的变化 int a,b; struct node { int x,y,t; friend bool operator < (node n1,node n2) //优先队列,按时间从小到大排列 { return n1.t>n2.t; } } s1,s2; void BFS() { priority_queue <node> q; //优先队列的写法 while(!q.empty()) q.pop(); s1.x=a; s1.y=b; s1.t=0; visit[a][b]=1; //起点标记为走过 q.push(s1); while(!q.empty()) { s1=q.top(); //与普通队列的不同 q.pop(); if(map[s1.x][s1.y]=='a') //结束标志找到了天使 { printf("%d\n",s1.t); return; } for(int i=0; i<4; i++) { s2.x=s1.x+f[i][0]; s2.y=s1.y+f[i][1]; if(s2.x>=0&&s2.x<n&&s2.y>=0&&s2.y<m&&!visit[s2.x][s2.y]&&map[s2.x][s2.y]!='#') //剪枝条件 { visit[s2.x][s2.y]=1; //走过的路标记为1 s2.t=s1.t+1; if(map[s2.x][s2.y]=='x') //若遇到‘x’,则需要两秒的时间 s2.t=s1.t+2; q.push(s2); } } } cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; //找不到的输出 return; } int main() { int i,j; while(~scanf("%d%d",&n,&m)) { for(i=0; i<n; i++) for(j=0; j<m; j++) { cin>>map[i][j]; if(map[i][j]=='r') //记录起点的位置 { a=i; b=j; } } memset(visit,0,sizeof(visit)); BFS(); } return 0; }
相关文章推荐
- iPhone UI笔试
- UVA 1584 Circular Sequence
- 为了UI,为了梦
- 如何发现交互设计中的思维盲区
- PAT 1062. Talent and Virtue (25)
- iOS 关于状态栏UIStatusBar的相关设置
- All of these replica devices about Diesel-engined include varied fashions right from antique
- 『MySQL』索引类型 normal, unique, full text
- DuiLib窗口增加阴影效果
- iOS基础-高级视图-UITableView--实例:QQ聊天
- UI, 基础控件,ToolBar,...
- hdu 1005 Number Sequence
- UIImageView设定圆角
- 【安卓学习问题日志】The project was not built due to "A resource exists with a different case
- ios实战开发之UIScrollView图片浏览器
- 很好的UI动效设计参考
- UITextField的键盘种类、用法总结
- UESTC cdoj 619 吴神,人类的希望 (组合数学)
- iOS开发-UIScrollView图片无限循环
- PL/SQL 报错:A query with LOB's requires OCI8 mode, but OCI7 mode is used