1. 程式人生 > >Tempter of the Bone——DFS(王道)

Tempter of the Bone——DFS(王道)

less for with test case Go rmi one 如果 坐標

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, 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
 
題目大意:還是迷宮的題目,S是起點,D是終點,X是墻,每秒一個位置,當好T秒走到
該題不再要求最優解,而是判定符合條件的路徑,因此用深度優先搜索。
裏面有關於剪枝的運用(因為如果不剪枝可能會超時),在54行,即判斷起始點和時間的關系。


 1
#include <iostream> 2 #include<cstdio> 3 using namespace std; 4 char maze[8][8];//保存地圖信息 5 int n,m,t;//地圖大小n*m,起點到終點能否恰好為t 6 bool success;//是否找到所需狀態標記 7 int go[][2]={//s四個方向行走坐標差 8 -1,0, 9 1,0, 10 0,1, 11 0,-1 12 }; 13 14 void DFS(int x,int y,int time){ 15 for(int i=0;i<4;i++){ 16 int nx=x+go[i][0];//枚舉四個相鄰位置 17 int ny=y+go[0][i]; 18 if(nx<1 || nx>n || ny<1 || ny>m)//地圖外 19 continue; 20 if(maze[nx][ny]==X)//碰墻 21 continue; 22 if(maze[nx][ny]==D){//到終點 23 if(time+1==t){//所用時間恰好為t 24 success=true;//搜索成功 25 return; 26 } 27 else 28 continue; 29 } 30 maze[nx][ny]=X;//該點設為墻 31 DFS(nx,ny,time+1);//遞歸擴展該狀態 32 maze[nx][ny]=.;//把原來的路改回來 33 if(success) 34 return; 35 } 36 } 37 38 int main(){ 39 while(scanf("%d %d %d",&n,&m,&t)!=EOF){ 40 if(n==0 && m==0 && t==0) 41 break; 42 for(int i=1;i<=n;i++) 43 scanf("%s",maze[i]+1); 44 success=false; 45 int sx,sy; 46 for(int i=1;i<=n;i++){//尋找D的坐標 47 for(int j=1;j<=m;j++){ 48 if(maze[i][j]==D){ 49 sx=i; 50 sy=j; 51 } 52 } 53 } 54 for(int i=1;i<=n;i++){//找到S後,判斷S和D的奇偶關系是否和t相符 55 for(int j=1;j<=m;j++){ 56 if(maze[i][j]==S && (i+j)%2==((sx+sy)%2+t%2)%2){ 57 maze[i][j]=X;//起始點設為墻 58 DFS(i,j,0);//遞歸擴展初始狀態 59 } 60 } 61 } 62 puts(success==true?"YES":"NO"); 63 } 64 return 0; 65 }

//說實話我真的不是很懂43行,為什麽要+1。。。請在評論區告訴我,多謝!

Tempter of the Bone——DFS(王道)