解题报告Tempter of the Bone(DFS)

Posted sixwater6h2o

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解题报告Tempter of the Bone(DFS)相关的知识,希望对你有一定的参考价值。

解题报告——Tempter of the Bon
  • 问题描述
Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d      Java class name: Main
 
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, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

‘X‘: a block of wall, which the doggie cannot enter; 
‘S‘: the start point of the doggie; 
‘D‘: the Door; or
‘.‘: an empty block.

The input is terminated with three 0‘s. This test case is not to be processed.

Output

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

Sample Input

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES


      大致意思就是给定一个迷宫,小狗在起点位置,不能停留也不能走回头路,问小狗能否在第t秒恰好走到出口(即走了t步恰好走到出口)

 

  • 解题思路

      是深度优先搜索+奇偶剪枝问题。使用递归让小狗一条路走到不能再走或到达出口,做的时候类比了全排列问题和n皇后问题,一步步枚举出所有可能的情况,同时在dfs函数中设置参数记录步数,再判断步数是否恰好等于要求的步数。

    并且对于这道题来说,因为记录了步数,所以再完成一种情况回到上一步的时候,除了要把标记过的地方还原外,步数还要减一。

      但直接深搜会超时,所以要用奇偶剪枝。

      奇偶剪枝就是当小狗需要绕行时,多走的路径一定是对称的(就是偏离了多少步就要走回来多少步),所以如果能在指定步数到达出口,指定步数(即小狗总共走的步数),减去假设没有障碍物的小狗到达终点的最短步数得到的步数差一定是偶数。如果步数差是奇数,则不需要进行深度搜索,直接输出“NO”即可。

  • 错误代码

      一开始以为是广度优先搜索,直接用了前一题走迷宫的代码改了改,本地跑的时候样例也过了但是提交的时候WA了,后来又学了学广搜和深搜的区别,发现广搜是求出最短路径的。

      如果这道题用广搜的话,小狗以一种岩浆蔓延的姿态向前前进,在第一次,也就是最短路径的时候就将终点处附近标记了,继续蔓延也不会访问已经被标记了的地方,这就导致了如果最短路径不是要求的步数,就不能求出步数大于最短路径但能到达终点的情况,也就无法找到正确答案。错误代码如下:

 1 #include<cstdio>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 queue <int> Q;
 6 char ls[8][8];
 7 int can[8][8];
 8 int dx[4]=-1,1,0,0;
 9 int dy[4]=0,0,-1,1;
10 int main()
11 
12     int N,M,T,i,j,x,y;
13     while (1)
14         char s[8];
15         int flag=0;
16         scanf("%d %d %d",&N,&M,&T);
17         memset(can,0,sizeof(can));
18         if (N==0&&M==0&&T==0) break;
19         for (i=0;i<N;i++)
20             scanf("%s",&s);
21             for (j=0;j<M;j++)
22                 ls[i][j]=s[j];
23                 if (s[j]==S)
24                     can[i][j]=1;
25                     Q.push(i*10000+j*100+0);
26                 
27                 if (s[j]==D)
28                     x=i;y=j;
29                 
30             
31         
32         while(!Q.empty())
33             if (flag==1) break;
34             int now=Q.front();
35             Q.pop();
36             int nowx=now/10000,nowy=now/100%100,step=now%100;
37             for (i=0;i<4;i++)
38                 int nx=nowx+dx[i],ny=nowy+dy[i];
39                 if (nx==x&&ny==y)
40                     if (step+1==T) flag=1;
41                     continue;
42                 
43                 if (nx<0||nx>=N||ny<0||ny>=M) continue;
44                 else if (can[nx][ny]==1||ls[nx][ny]==X) continue;
45                 else
46                     Q.push(nx*10000+ny*100+(step+1));
47                     can[nx][ny]=1;
48                 
49             
50         
51         if (flag==0) printf("NO\n");
52         else printf("YES\n");
53     
54     return 0;
55 

 

  • AC代码
 1 #include<cstdio>
 2 #include<string.h>
 3 #include<cstdlib>
 4 using namespace std;
 5 char ls[10][10];
 6 int can[10][10];
 7 int N,M,T,i,j,xend,yend,flag,xstart,ystart;
 8 char s[10];
 9 int dxy[4][2]=-1,0,1,0,0,-1,0,1;
10 void dfs(int x,int y,int step)
11 
12     if (x==xend&&y==yend)
13         if (step==T)
14             flag=1;
15             return ;
16         
17         return;
18     
19     if (flag==1) return;
20     for (int i=0;i<4;++i)
21         int xx=x+dxy[i][0];
22         int yy=y+dxy[i][1];
23         if (xx<0||xx>=N||yy<0||yy>=M) continue;
24         else if (can[xx][yy]==1||ls[xx][yy]==X) continue;
25         can[xx][yy]=1;
26         dfs(xx,yy,++step);
27         can[xx][yy]=0;
28         step--;
29     
30 
31 int main()
32 
33     while (1)
34         char s[8];
35         flag=0;
36         scanf("%d %d %d",&N,&M,&T);
37         memset(can,0,sizeof(can));
38         if (N==0&&M==0&&T==0) break;
39         for (i=0;i<N;i++)
40             scanf("%s",&s);
41             for (j=0;j<M;j++)
42                 ls[i][j]=s[j];
43                 if (s[j]==S)
44                     xstart=i;ystart=j;
45                     can[i][j]=1;
46                 
47                 if (s[j]==D)
48                     xend=i;yend=j;
49                 
50             
51         
52         if ((T-abs(xend-xstart)-abs(yend-ystart))%2!=0||T<abs(xend-xstart)+abs(yend-ystart))
53             printf("NO\n");
54             continue;
55         
56         dfs(xstart,ystart,0);
57         if (flag==0) printf("NO\n");
58         else printf("YES\n");
59     
60     return 0;
61 

      这道题还有一个值得注意的地方就是在dfs找到符合条件的解后将flag设为1后,直接在dfs函数中加入

      if (flag==1) return;

      的语句,可以再次减少后续不必要的查找,进一步缩短时间。

总而言之还不是难题但架不住我菜()

以上是关于解题报告Tempter of the Bone(DFS)的主要内容,如果未能解决你的问题,请参考以下文章

题解报告:hdu 1010 Tempter of the Bone

hdu1010Tempter of the Bone(dfs+奇偶剪枝)

hdu1010 Tempter of the Bone---DFS+奇偶剪枝

Tempter of the bone

hdu 1010 Tempter of the Bone

Tempter of the Bone