hdu 1242 Rescue【bfs+优先队列(c语言模拟)】

来源:互联网 发布:python twisted 教程 编辑:程序博客网 时间:2024/06/05 02:31


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 210#define inf 0x3f3f3f3fchar map[N][N];int book[N][N],m,n,si,sj,ans;struct node{int x,y,time;}q[N*N];int cmp(struct node a,struct node b){return a.time < b.time ;}void bfs(){int i,j,flag;int k[4][2] = {0,1,0,-1,1,0,-1,0};int tail,head;struct node now;ans = inf;tail = head = 0;//c语言模拟队列 q[tail].x = si;q[tail].y = sj;q[tail].time = 0;tail ++;flag = 0;while(head < tail){for(i = 0; i < 4; i ++){now.x = q[head].x + k[i][0];now.y = q[head].y + k[i][1];now.time = q[head].time ;if(now.x < 1||now.y < 1||now.x > n||now.y > m)continue;if(!book[now.x][now.y]&&map[now.x][now.y]!='#'){book[now.x][now.y] = 1;if(map[now.x][now.y] == 'x')//遇到x时间+2 now.time = now.time +2;elsenow.time = now.time +1;q[tail].x = now.x ;q[tail].y = now.y ;q[tail++].time = now.time ;if(map[q[tail-1].x][q[tail-1].y] == 'r'){ans = q[tail-1].time ;//遇到终点就结束查找 flag = 1;}}}head ++;sort(q,q+tail,cmp);//模拟优先队列if(flag)break;} return;}int main(){int j,i;while(scanf("%d%d",&n,&m)!=EOF){memset(book,0,sizeof(book));for(i = 1; i <= n; i ++){scanf("%s",map[i]+1);for(j = 1; j <= m; j ++){if(map[i][j] == 'a'){si = i;sj = j;book[i][j] = 1;}}}bfs();if(ans == inf)printf("Poor ANGEL has to stay in the prison all his life.\n");elseprintf("%d\n",ans);}return 0;}

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32607    Accepted Submission(s): 11400


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.)
 

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的最短时间,找不到,输出"Poor ANGEL has to stay in the prison all his life." 。遇到'.',时间+1,遇到‘x’,时间+2.
思路:由于r有多个,所以我们从a找r,要求最短时间,那么我们在搜索时,每走下一步,就需要找到时间最小的那步,所以用到c++的优先队列,不过这次是用c模拟的优先队列。

 
原创粉丝点击