hdu 1242 不用标记数组的深搜

#include<stdio.h>
#include<string.h>
char mapp[220][220];
int m,n,mmin;
void dfs(int x,int y,int time){
  if(mapp[x][y]=='#'){
   return ;
  }
  if(x<0||x>=m||y<0||y>=n){
   return ;
  }
  if(mapp[x][y]=='a'){
         dfs(x,y+1,0);
         dfs(x,y-1,0);
         dfs(x+1,y,0);
         dfs(x-1,y,0);
     }
  if(mapp[x][y]=='r'){
   if(mmin>time){
    mmin=time;
   }
  }
  if(mapp[x][y]=='x'){
   mapp[x][y]='#';
   dfs(x+1,y,time+2);
   dfs(x-1,y,time+2);
   dfs(x,y+1,time+2);
   dfs(x,y-1,time+2);
   mapp[x][y]='x';
  }
  if(mapp[x][y]=='.'){
   mapp[x][y]='#';
   dfs(x+1,y,time+1);
   dfs(x-1,y,time+1);
   dfs(x,y+1,time+1);
   dfs(x,y-1,time+1);
   mapp[x][y]='.';
  }
}
int main(){
 int i,j;
 while(scanf("%d %d",&m,&n)!=EOF){
  for(i=0;i<m;i++){
   scanf("%s",mapp[i]);
  }
  mmin=99999;
  for(i=0;i<m;i++){
   for(j=0;j<n;j++){
    if(mapp[i][j]=='a'){
     dfs(i,j,0);
    }
   }
  }
  if(mmin!=99999){
   printf("%d ",mmin+1);
  }
  else{
   printf("Poor ANGEL has to stay in the prison all his life. ");
  } 
 }
 return 0;
}

这道题目用正常的深搜去做的话会超时。。。  在讨论区看到别人这样用  挺有意思的。。

通过改变mapp的值来代替标记 回溯的过程    有意思。。

还有 对于大规模的输入  最好用scanf。。。。。。 不要轻易的使用scanf("%c")..  特别是有回车符的时候。。。  很危险。。。。

原文地址:https://www.cnblogs.com/z1141000271/p/5414712.html