HDOJ 1242   Rescue

来源:互联网 发布:苹果电脑设计软件下载 编辑:程序博客网 时间:2024/05/20 03:46
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
代码:
#include
#include
#include
#include
#include
usingnamespace std;
const intMAXN=200;
const intINF=10000000;
typedefstruct points{
   int x,y;
   int step;
   int time;
}point;
queueQ;
intN,M;
charmap[MAXN][MAXN];
intmintime[MAXN][MAXN];
intdir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
intax,ay;
intBFS(point s){
   int i;
   Q.push(s);
   point hd;
   while(!Q.empty()){
      hd=Q.front();
      Q.pop();
      for(i=0;i<4;i++){
          intx=hd.x+dir[i][0],y=hd.y+dir[i][1];
         if(x>=0&&x<=N-1&&y>=0&&y<=M-1&&map[x][y]!='#'){
             point t;
             t.x=x;
             t.y=y;
             t.step=hd.step+1;
             t.time=hd.time+1;
             if(map[x][y]=='x'){
                t.time++;
             }
             if(t.time
                mintime[x][y]=t.time;
                Q.push(t);
             }
          }
      }
   }
   return mintime[ax][ay];
}
intmain(){
   int i,j;
  //freopen("C:\\Users\\acm\\Desktop\\001.in","r",stdin);
  while(scanf("%d%d",&N,&M)!=EOF){
      memset(map,0,sizeof(map));
      for(i=0;i
         scanf("%s",map[i]);
      }
       intsx,sy;
       pointstart;
      for(i=0;i
          for(j=0;j
             mintime[i][j]=INF;
             if(map[i][j]=='a'){
                ax=i,ay=j;
             }
             else if(map[i][j]=='r'){
                sx=i,sy=j;
             }
          }
      }
      start.x=sx,start.y=sy;
      start.step=0,start.time=0;
      mintime[sx][sy]=0;
       intmint=BFS(start);
      if(mint
         printf("%d\n",mint);
      }
      else{
          printf("Poor ANGEL has tostay in the prison all his life.\n");
      }
   }
   return 0;
}

0 0
原创粉丝点击