概述
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27043 Accepted Submission(s): 9576
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
13bfs迷宫题,学会使用结构体和方向数组使用优先队列求得最优解~读入图的过程要注意。#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<queue> using namespace std; #define Maxn 205 char mat[Maxn][Maxn]; int vis[Maxn][Maxn]; int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int row,col; struct Node { int x,y; int time; friend bool operator < (const Node &a,const Node &b) { return a.time>b.time; } }; Node start; int bfs(Node source) { memset(vis,0,sizeof(vis)); priority_queue<Node> q; while(!q.empty()) q.pop(); q.push(source); vis[source.x][source.y]=1; while(!q.empty()) { int i; Node next; Node a=q.top(); q.pop(); for(i=0;i<4;i++) { next.x=a.x+dir[i][0]; next.y=a.y+dir[i][1]; if(next.x<0||next.x>=row||next.y<0||next.y>=col||mat[next.x][next.y]=='#') continue; if(vis[next.x][next.y]) continue; vis[next.x][next.y]=1; if(mat[next.x][next.y]=='x') next.time=a.time+2; if(mat[next.x][next.y]=='.') next.time=a.time+1; if(mat[next.x][next.y]=='r') {return a.time+1;} q.push(next); } } return -1; } int main() { int i,j; int ans; while(scanf("%d%d",&row,&col)!=EOF) { for(i=0;i<row;i++) { for(j=0;j<col;j++) { cin>>mat[i][j]; //scanf("%c",&mat[i][j]);为什么用scanf不行??? if(mat[i][j]=='a') { start.x=i; start.y=j; start.time=0; } } getchar(); } /*for(i=0;i<row;i++) { for(j=0;j<col;j++) { cout<<mat[i][j]; cout<<endl; }*/ ans=bfs(start); if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.n"); else printf("%dn",ans); } return 0; }
最后
以上就是无辜悟空为你收集整理的HDU1242Rescue的全部内容,希望文章能够帮你解决HDU1242Rescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复