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\n",mmin+1);
  }
  else{
   printf("Poor ANGEL has to stay in the prison all his life.\n");
  } 
 }
 return 0;
}

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

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

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

时间: 2024-08-06 11:50:39

hdu 1242 不用标记数组的深搜的相关文章

HDU 1016 Prime Ring Problem(深搜)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 32872    Accepted Submission(s): 14544 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

hdu 1010 Tempter of the Bone 深搜+剪枝

题意:在一个坐标内,给定起点和终点,问能否恰好在t时刻到达终点. 以前很少写搜索题,所以看到这个题,就按照普通的深搜写了一下,交上去超时了.后来在网上搜了一下才知道,要剪枝才行.可是,我以前从没写过剪枝,不知道怎么剪,就按照别人的思路往下想.看懂以后,我对剪枝的理解是:对于一些没有必要继续搜索的路径,不再往下深搜,提前返回到上一层.花了半天时间调试代码,终于AC了. 奇偶剪枝:根据题目,doggie必须在第t秒到达门口.也就是需要走t-1步.设doggie开始的位置为(sx,sy),目标位置为(

HDU 1045 Fire Net【DFS深搜】

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9880    Accepted Submission(s): 5739 Problem Description Suppose that we have a square city with straight streets. A map of a city is a

hdu 1015(Safecracker)(深搜)

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8782    Accepted Submission(s): 4443 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klei

HDU - 1010 Tempter of the Bone 深搜模板题(DPS)解题报告

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 88587    Accepted Submission(s): 24116 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(深搜)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 86037    Accepted Submission(s): 23462 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu 1312 Red and Black(深搜)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10286    Accepted Submission(s): 6426 Problem Description There is a rectangular room, covered with square tiles. Each tile is colo

HDU 1010 Temper of the bone(深搜+剪枝)

Tempter of the Bone Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap

HDU 1312 Red and Black【深搜】

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11531    Accepted Submission(s): 7177 Problem Description There is a rectangular room, covered with square tiles. Each tile is colo