代码改变世界

hdu1242 优先队列加广搜

2012-03-28 13:10  璋廊  阅读(213)  评论(0编辑  收藏  举报
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,所以我们就从a搜到r;
优先队列只是定义不同,用的时候和普通队列差不多:
代码如下:
#include<stdio.h>
#include<queue>
using namespace std;
struct point 
{
 int x,y,step;
 friend bool operator<(point a,point b)//定义一个优先队列
 {
  return a.step>b.step;
 }
};
int dir[4][2]={1,0,-1,0,0,1,0,-1};
char map[200][200];
point cut,next;
point start;
int x1,y1,n,m;
int dfs(point start)
{
 int i;
 priority_queue<point>q;
 cut=start;
 q.push(cut);
 while(!q.empty())
 {
  cut=q.top();//注意此处
  q.pop();
  for(i=0;i<4;i++)
  {
   next.x=cut.x+dir[i][0];
   next.y=cut.y+dir[i][1];
   if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&map[next.x][next.y]!='#')
   {
    if(map[next.x][next.y]=='.')
    {
     map[next.x][next.y]='#';
     next.step=cut.step+1;
     q.push(next);
     continue;
    }
    if(map[next.x][next.y]=='r')
     return next.step=cut.step+1;
    if(map[next.x][next.y]=='x')
    {
     map[next.x][next.y]='#';
      next.step=cut.step+2;
     q.push(next);
     continue;
    }
   }
  }
 }
  return -1;
}                                                                                                                                                                                                                                                                                                                                                                                                                           
int main()
{
 int i,j;
 while(scanf("%d%d",&n,&m)!=EOF)
 {
 // pp=0;
  for(i=0;i<n;i++)
   scanf("%s",map[i]);
  for(i=0;i<n;i++)
   for(j=0;j<m;j++)
   {
    //scanf("%c",&map[i][j]);
    if(map[i][j]=='a')
    {
     x1=i;
     y1=j;
     map[i][j]='#';
     break;
    }
   }
   start.x=x1;
   start.y=y1;
   start.step=0;
  int k=dfs(start);
   if(k!=-1)
    printf("%d\n",k);
   else 
   printf("Poor ANGEL has to stay in the prison all his life.\n");
 }
 return 0;
}