HDU 1242 Rescue(营救)(优先队列+BFS)
2014-07-29 20:56
295 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
Total Submission(s): 15880 Accepted Submission(s): 5756
[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
刚开始一直在超时,原因是输入哪里没有加一个~这个符号,弄了好久,=.=,好无语
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15880 Accepted Submission(s): 5756
[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
刚开始一直在超时,原因是输入哪里没有加一个~这个符号,弄了好久,=.=,好无语
#include <iostream> #include <stdio.h> #include <queue> #include <string.h> using namespace std; int m,n; char mp[220][220]; int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; typedef struct node { int x,y, step; }; bool operator < (const node &p,const node &q) { return p.step > q.step; } int judge(int x,int y) { if(x<0 || y<0 ||x>=m ||y>=n || mp[x][y]=='#') return 0; return 1; } int bfs(int row,int col) { priority_queue <node> qu; node s,out; s.x = row; s.y = col; s.step = 0; qu.push(s); mp[row][col]='#'; while(!qu.empty()) { out = qu.top(); qu.pop(); // printf("x= %d,y= %d ,step = %d %c\n",out.x,out.y,out.step,mp[out.x][out.y]); for(int i=0; i<4; i++) { int x = out.x+dir[i][0]; int y = out.y+dir[i][1]; if(judge(x,y)) { if(mp[x][y] == 'a') { printf("%d\n",out.step+1); return 1; } s.x = x; s.y = y; s.step = out.step+1; if(mp[x][y]=='x') s.step++; qu.push(s); mp[x][y]='#'; } } } return 0; } /* 7 8 #.#####. #..#..r. #.a#x... ..#..#.# #...##.. .#...... ........ */ int main() { while(~scanf("%d %d",&m,&n)) { int c,d; int i,j; for(i=0; i<m; i++) { scanf("%s",mp[i]); for(j=0; j<n; j++) { if(mp[i][j]=='r') { c=i; d=j; } } getchar(); } // printf("mp[1][2]=%c\n",mp[1][2]); int flag = bfs(c,d); if(flag==0) printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0; } //http://acm.hdu.edu.cn/showproblem.php?pid=1242
相关文章推荐
- HDU 1242 Rescue(BFS,优先队列)
- HDU-1242-Rescue(优先队列+BFS)
- hdu-1242Rescue(优先队列+bfs)
- BFS:HDU-1242-Rescue(带守卫的迷宫问题)(优先队列)
- hdu-1242Rescue(bfs搜索+优先队列)
- hdu1242 Rescue 优先队列BFS
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(优先队列+bfs)
- hdu 1242 Rescue(BFS优先队列)
- hdu 1242:Rescue(BFS广搜 + 优先队列)
- Hdu 1242 & Zoj 1649 Rescue (优先队列+BFS)
- hdu 1242 Rescue 搜索 bfs 优先队列
- HDU 1242 - Rescue(BFS 优先队列)
- hdu 1242 Rescue(bfs)
- 【搜索入门专题1】 hdu1242 J - Rescue c++ stl容器之优先队列+BFS
- hdu 1242 Rescue(优先队列 && 广搜BFS)
- HDU 1242 Rescue 简单bfs 好题
- HDU1242 Rescue 解题报告--bfs
- 1242Rescue (优先队列BFS)
- HDU-1242 Rescue( 广搜 + 优先队列)