zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】
2015-03-20 22:36
585 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649
RescueTime Limit: 2 Seconds Memory Limit: 65536 KB
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.)
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.
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
题意:从'r'起步,目的地为'a','#'不可过,'.'为一步,特殊的是‘x'算两步。求最少步数。
思路:bfs。这里需要注意的是x,因为算两步,在普通的bfs队列里不一定先出列的是最短步,所以得用优先队列。
代码:
View Code
RescueTime Limit: 2 Seconds Memory Limit: 65536 KB
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.)
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.
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
题意:从'r'起步,目的地为'a','#'不可过,'.'为一步,特殊的是‘x'算两步。求最少步数。
思路:bfs。这里需要注意的是x,因为算两步,在普通的bfs队列里不一定先出列的是最短步,所以得用优先队列。
代码:
#include <fstream> #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <queue> using namespace std; #define PI acos(-1.0) #define EPS 1e-10 #define lll __int64 #define ll long long #define INF 0x7fffffff struct node{ int x,y,step; bool operator < (const node &r) const{ return step>r.step; } }tmp; priority_queue<node> qu; int n,m,x1,y1_; char matrix[205][205]; bool b[205][205]; char xy[2][4]={{0,0,1,-1},{1,-1,0,0}}; inline bool Check(int x,int y); int Bfs(); int main(){ //freopen("D:\\input.in","r",stdin); //freopen("D:\\output.out","w",stdout); while(~scanf("%d %d",&n,&m)){ for(int i=0;i<=m+1;i++) matrix[0][i]='#',matrix[n+1][i]='#'; for(int i=0;i<=n+1;i++) matrix[i][0]='#',matrix[i][m+1]='#'; for(int i=1;i<=n;i++){ getchar(); for(int j=1;j<=m;j++){ matrix[i][j]=getchar(); if(matrix[i][j]=='r') x1=i,y1_=j; } } int ans=Bfs(); if(ans==-1) puts("Poor ANGEL has to stay in the prison all his life."); else printf("%d\n",ans); } return 0; } inline bool Check(int x,int y){ return matrix[x][y]!='#'&&b[x][y]==0; } int Bfs(){ memset(b,0,sizeof(b)); while(!qu.empty()) qu.pop(); tmp.step=0; tmp.x=x1; tmp.y=y1_; qu.push(tmp); b[x1][y1_]=1; while(!qu.empty()){ tmp=qu.top(); qu.pop(); int x=tmp.x,y=tmp.y; int tx,ty,ts=tmp.step+1; for(int i=0;i<4;i++){ tx=x+xy[0][i]; ty=y+xy[1][i]; if(Check(tx,ty)){ if(matrix[tx][ty]=='.'){ b[tx][ty]=1; tmp.x=tx; tmp.y=ty; tmp.step=ts; qu.push(tmp); }else if(matrix[tx][ty]=='x'){ b[tx][ty]=1; tmp.x=tx; tmp.y=ty; tmp.step=ts+1; qu.push(tmp); }else{ return ts; } } } } return -1; }
View Code
相关文章推荐
- zoj 1649 Rescue【BFS+优先队列】
- 迷宫最短路径的C++实现(队列:广度优先)
- 迷宫最短路径的C++实现(队列:广度优先)
- (广度优先搜索第一课)迷宫的最短路径 - BFS
- 广度优先搜索--迷宫最短路径--队列
- HDU 1242 Rescue【BFS+优先队列】
- BFS_Maze_求解迷宫最短路径
- bfs之迷宫最短路径
- 迷宫最短路径算法(使用队列)
- POJ 3984 迷宫问题(BFS:迷宫最短路径且输出路径)
- ZOJ-1649 Rescue BFS (HDU 1242)
- BFS循环求迷宫最短路径及路径数
- 重学搜索yi.3:迷宫的最短路径--bfs
- 广度优先搜索 最短路径 队列
- 最短路径Dijkstra算法-优先队列优化
- 迷宫问题(广度优先搜索,输出最短路径)
- BFS求解迷宫最短路径
- HDU 1242 Rescue(优先队列 + 多源BFS)
- hdu 1242 rescue (优先队列 bfs)
- 仙岛求药(BFS迷宫寻找最短路径)