首页 > 代码库 > 2E02-View-Lists-multiple -choice-list
2E02-View-Lists-multiple -choice-list
Rescue
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
Author
CHEN, Xue
Source
ZOJ Monthly, October 2003
一个地图中,给定起点和终点,上下左右走,每走一步花1个单位时间,遇到怪,需要杀死怪,额外花1个单位时间,求从起点到终点最少花多少分钟。
主要思路是广度优先搜索,但因为其中有怪,需要额外花时间,那么对于队列里面的每个节点(保存x,y坐标和当前所用时间),并不是“公平”的,要想最少花时间,那么每次在队头取出的元素应该是队列里面所用时间最少的,所以应该用优先队列。
代码:
#include <iostream> #include <queue> #include <string.h> #include <algorithm> using namespace std; const int maxn=210; char mp[maxn][maxn]; bool vis[maxn][maxn]; int dx[4]={0,1,-1,0}; int dy[4]={-1,0,0,1}; int n,m;//地图大小 int sx,sy;//起点 int ex,ey;//终点 struct Node { int x,y; int step; }node; bool operator<(Node a,Node b)//定义结构体类型的优先队列的优先级,step小的优先 { return a.step>b.step; } void getMap(int n,int m) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { cin>>mp[i][j]; if(mp[i][j]=='r') { sx=i; sy=j; } if(mp[i][j]=='a') { ex=i; ey=j; } } } bool judge(int x,int y)//判断x,y是否可以到达 { if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]&&mp[x][y]!='#') return true; return false; } int bfs(int x,int y) { memset(vis,0,sizeof(vis)); priority_queue<Node>q; Node a,b; a.x=x,a.y=y,a.step=0; q.push(a); while(!q.empty()) { b=q.top(); q.pop(); for(int i=0;i<4;i++) { int nextx=b.x+dx[i]; int nexty=b.y+dy[i]; if(judge(nextx,nexty)) { vis[nextx][nexty]=1; a.x=nextx; a.y=nexty; if(mp[nextx][nexty]=='x') { a.step=b.step+2; q.push(a); } else { a.step=b.step+1; q.push(a); if(nextx==ex&&nexty==ey)//找到就返回 不用接着找了 return a.step; } } } } return -1; } int main() { while(cin>>n>>m) { getMap(n,m); int 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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。