hdu 1242 우선 대기 열 및 비 우선 대기 열 2 중 방법

http://acm.hdu.edu.cn/showproblem.php?pid=1242
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

/* : n×m , a, r。# ,. 。 x 。r , , 1 。 x , 1 x。 r a

: 。 。

 

 

       AC                 #include<stdio.h> #include<string.h> int a[205][205],s_x,s_y,e_x,e_y,flag[205][205]; int b[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; struct haha {  int x;  int y;  int ti; }que[1000000]; int bfs() {        int head=0,tail=1,min=100000000,i;     que[1].x=s_x;que[1].y=s_y;que[1].ti=0;flag[s_x][s_y]=1;     while(head<tail)     {      head++;      if(que[head].ti>min) continue;      if(que[head].x==e_x&&que[head].y==e_y)      {       if(min>que[head].ti) min=que[head].ti;continue;      }       for(i=0;i<4;i++)      if(a[que[head].x+b[i][0]][que[head].y+b[i][1]]!=0&&flag[que[head].x+b[i][0]][que[head].y+b[i][1]]==0)      {                   flag[que[head].x+b[i][0]][que[head].y+b[i][1]]=1;                   que[++tail].x=que[head].x+b[i][0];       que[tail].y=que[head].y+b[i][1];       que[tail].ti=que[head].ti+a[que[head].x+b[i][0]][que[head].y+b[i][1]];      }     }  return min;    } int main() {  int hang,lie,i,j,ch,ans;  while(scanf("%d %d",&hang,&lie)!=EOF)  {   getchar();   memset(a,0,sizeof(a));   memset(flag,0,sizeof(flag));   for(i=1;i<=hang;i++)   {    for(j=1;j<=lie;j++)    {     ch=getchar();     if(ch=='#') a[i][j]=0;     if(ch=='.') a[i][j]=1;     if(ch=='x') a[i][j]=2;     if(ch=='a'){s_x=i;s_y=j;a[i][j]=1;}     if(ch=='r'){e_x=i;e_y=j;a[i][j]=1;}    }              getchar();   }   ans=bfs();   if(ans==100000000)  printf("Poor ANGEL has to stay in the prison all his life.
");   else         printf("%d
",ans);  } }

 

 

 

#include<stdio.h> #include<string.h> #include<queue> using namespace std; int a[205][205],e_x,e_y; int b[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; struct haha {  int x;  int y;  int ti;  bool operator <(const haha &a) const //a  {   return ti>a.ti;// > <  } }q,temp,end;

priority_queue<haha>duilie;//   duilie

int bfs() {        int i;     int tx,ty;     while(!duilie.empty())     {      temp=duilie.top();      duilie.pop();      for(i=0;i<4;i++)      {       tx=temp.x+b[i][0];       ty=temp.y+b[i][1];       if(a[tx][ty]!=0)       {        q.x=tx;        q.y=ty;        q.ti=temp.ti+a[tx][ty];        duilie.push(q);        if(tx==e_x&&ty==e_y)  return temp.ti+1;        a[tx][ty]=0;       }      }     }     return -1; } int main() {  int hang,lie,i,j,ch,ans;  while(scanf("%d %d",&hang,&lie)!=EOF)  {   getchar();   memset(a,0,sizeof(a));     while(!duilie.empty())      duilie.pop();       for(i=1;i<=hang;i++)   {    for(j=1;j<=lie;j++)    {     ch=getchar();     if(ch=='#') a[i][j]=0;     if(ch=='.') a[i][j]=1;     if(ch=='x') a[i][j]=2;     if(ch=='a')     {                     q.x=i;      q.y=j;      q.ti=0;      duilie.push(q);      a[i][j]=1;     }     if(ch=='r'){e_x=i;e_y=j;a[i][j]=1;}    }              getchar();   }   ans=bfs();   if(ans==-1)  printf("Poor ANGEL has to stay in the prison all his life.
");   else         printf("%d
",ans);  }  return 0; }

좋은 웹페이지 즐겨찾기