hd1242 Rescue (bfs+队列优先)
来源:互联网 发布:js调用打印机直接打印 编辑:程序博客网 时间:2024/06/14 03:10
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26365 Accepted Submission(s): 9322
Problem Description
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.)
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.)
Input
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.
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.
Output
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."
Sample Input
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
Sample Output
13
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxn = 210;char map[maxn][maxn];int vis[maxn][maxn];int n, m, i, j, sx, sy, ex,ey;int dx[4] = {1,0,-1,0};int dy[4] = {0,1,0,-1};struct node{ int x; int y; int step; bool friend operator <(node a,node b) { return a.step > b.step; }};int bfs(int x, int y){ int falg = 0; node now,next; priority_queue<node > q; now.x = x; now.y = y; now.step = 0; q.push(now); while(!q.empty()) { next = q.top(); q.pop(); if(next.x == ex && next.y == ey) { falg = 1; break; } for(i = 0 ; i < 4 ; i++) { now.x = next.x + dx[i]; now.y = next.y + dy[i]; if(now.x >=0 && now.x < n && now.y >= 0 && now.y < m && !vis[now.x][now.y] && map[now.x][now.y] != '#') { vis[now.x][now.y] = 1; if(map[now.x][now.y] == 'x') now.step = next.step + 2; else now.step = next.step + 1; q.push(now); } } } if(falg) return next.step; else return -1;}int main(){ int ans = 0; while(cin >> n >> m){ memset(vis,0,sizeof(vis)); memset(map,'\0',sizeof(map)); for(i = 0 ; i < n ; i++) cin >> map[i]; for(i = 0 ; i < n ; i++) for(j = 0 ; j < m ; j++) { if(map[i][j] == 'r') { sx = i; sy = j; } if(map[i][j] == 'a') { ex = i; ey = j; } } vis[sx][sy] = 1; ans = bfs(sx,sy); if(ans == -1) cout << "Poor ANGEL has to stay in the prison all his life." << endl; else cout << ans << endl; } return 0;}
0 0
- hd1242 Rescue (bfs+队列优先)
- Rescue (BFS 优先队列)
- Rescue(优先队列+bfs)
- rescue(BFS+优先队列)
- hdu 1242Rescue(bfs+优先队列)
- HDU1242 Rescue(优先队列加BFS)
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(BFS +优先队列)
- 1242Rescue (优先队列BFS)
- HDU 1242 Rescue(BFS,优先队列)
- hdu 1242 Rescue(BFS优先队列)
- HDU 1242 Rescue(优先队列+bfs)
- hdu 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(BFS+优先队列)
- hdoj 1242 Rescue (bfs+优先队列)
- hdu 1242 Rescue (BFS+优先队列)
- HDU 1242 Rescue (BFS+优先队列)
- hdu1242 Rescue(优先队列bfs)
- 001——初识Spring
- ubuntu_16.04_LTS使用NDK编译FFMPEG_3.1.1
- VS2010下MFC串口编程
- 自定义圆形里面内嵌文字,文字可根据圆形大小自适应
- C++学习小记(2016.7.29)
- hd1242 Rescue (bfs+队列优先)
- HDU 1849-Rabbit and Grass(尼姆博弈)
- 访问虚拟机上的本地网站
- windows安装python的mysql驱动mysql-connector-python
- 最全颜色表
- 【HDU】2157 - How many ways??(矩阵快速幂)(从i到j经过k个点有多少种走法)
- 解决android帮助文档打开慢
- js实现页面埋点
- Mysql远程不允许连接(Windows)