首页 > 代码库 > hdu 1242 Rescue

hdu 1242 Rescue

Rescue

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


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
 
优先队列解决
 
 
 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstdlib> 5 #include<cstring> 6 #include<queue> 7 using namespace std; 8  9 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};10 char map[205][205];11 int visit[205][205];12 int a,b;13 14 struct node{15     int x;16     int y;17     int time;18     friend bool operator < (const node &a,const node &b)19     {20        return a.time>b.time;21     }22 };23 24 int border(int x,int y)25 {26     if( (x>=1&&x<=a) && (y>=1&&y<=b) && (map[x][y]!=#) )27     return 1;28     return 0;29 }30 int bfs(int x,int y)31 {32     int i;33     node abc,q;34     priority_queue<struct node>p;35     memset(visit,0,sizeof(visit));36     abc.x=x;37     abc.y=y;38     abc.time=0;39     visit[abc.x][abc.y]=1;40     p.push(abc);41     while(!p.empty())42     {43         abc=p.top();44         p.pop();45         if(map[abc.x][abc.y]==r)46         {47             return abc.time;48         }49         for(i=0;i<4;i++)50         {51             q.x=abc.x+dir[i][0];52             q.y=abc.y+dir[i][1];53             if( border(q.x,q.y) && !visit[q.x][q.y])54             {55                 visit[q.x][q.y]=1;56                 if(map[q.x][q.y]==x)57                 q.time=abc.time+2;58                 else59                 q.time=abc.time+1;60                 p.push(q);61             }62         }63     }64     return -1;65 }66 67 int main()68 {69     //freopen("in.txt","r",stdin);70     int x,y,ans;71     int i,j;72     while(~scanf("%d%d",&a,&b))73     {74         for(i=1;i<=a;i++)75         {76             getchar();77             for(j=1;j<=b;j++)78             {79                 scanf("%c",&map[i][j]);80                 if(map[i][j]==a)81                 {82                     x=i;83                     y=j;84                 }85             }86         }87         ans=bfs(x,y);88         if(ans==-1)89         printf("Poor ANGEL has to stay in the prison all his life.\n");90         else91         printf("%d\n",ans);92 93     }94 95     return 0;96 }
View Code

 

 

hdu 1242 Rescue