Rescue(HD1242)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242

代码:

#include<iostream>
#include<queue>
#include<memory.h>
using namespace std;
int row,col;
char map[201][201];
int startX,startY,endX,endY;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct Node
{
  int x,y,count;
};
int visit[201][201];
Node N,P;
int minvalue;
void bfs()
{
    //初始化信息
   minvalue=100000;
   queue<Node> q;
   N.x=startX;
   N.y=startY;
   N.count=0;
   q.push(N);

   while(!q.empty())
   {
       N=q.front();
       q.pop();

       if(map[N.x][N.y]=='r')
           minvalue=min(N.count,minvalue);

      for(int i=0;i<4;i++)
      {
          int tx=N.x+dir[i][0];
          int ty=N.y+dir[i][1];

          if(tx>=0&&tx<row&&ty>=0&&ty<col&&map[tx][ty]!='#')
          {
             if(visit[tx][ty]>N.count+1)
            {
              P.x=tx;
              P.y=ty;
              P.count=N.count+1;
              if(map[tx][ty]=='x')
                P.count++;
                visit[tx][ty]=P.count;
               q.push(P);
            }
          }
      }
   }
}
int main()
{
  while(cin>>row>>col)
  {
    memset(visit,0,sizeof(visit));
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
        {
          visit[i][j]=100000;
          cin>>map[i][j];
          if(map[i][j]=='a')
          {
             startX=i;
             startY=j;
          }
          if(map[i][j]=='r')
          {
             endX=i;
             endY=j;
          }
        }
    bfs();
    if(minvalue!=100000)
        cout<<minvalue<<endl;
    else
        cout<<"Poor ANGEL has to stay in the prison all his life." <<endl;

  }

}

 

posted @ 2013-05-24 21:45  supersnow0622  Views(150)  Comments(0)    收藏  举报