hdu1010 Tempter of the Bone (DFS)


 

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33445    Accepted Submission(s): 9015


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,問你是否能在 T 時刻恰好到達終點D。
分析:這樣一看很明顯是DFS,不過里面涉及到很多剪枝。
 
奇偶剪枝:
是數據結構的搜索中,剪枝的一種特殊小技巧。
現假設起點為(sx,sy),終點為(ex,ey),給定t步恰好走到終點,
 
s        
|        
|        
|        
+ e
 
如圖所示(“|”豎走,“—”橫走,“+”轉彎),易證abs(ex-sx)+abs(ey-sy)為此問題類中任意情況下,起點到終點的最短步數,記做step,此處step1=8;
  
s  
  +  
| +      
|        
+ e
 
如圖,為一般情況下非最短路徑的任意走法舉例,step2=14;
step2-step1=6,偏移路徑為6,偶數(易證);
故,若t-[abs(ex-sx)+abs(ey-sy)]結果為非偶數(奇數),則無法在t步恰好到達;
返回,false;
反之亦反。
 
心得:起初沒仔細看題,以為是 T 時間內到達的,果斷 BFS 呀,於是果斷 WA ,於是就把題目在仔細看了一遍,沒想到是 T 時刻到達,
所以心涼了一截,再次果斷 DFS ,稍微剪下枝,就交了TLE,無語了,想了好久沒不知道怎么剪枝的,后來看了下解題報告才
知道還有個奇偶剪枝,挺強大的。
以后要仔細看題
 
View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #define N 10
 4 
 5 using namespace std;
 6 
 7 int n,m,t,end_i,end_j;
 8 bool visited[N][N],flag,ans;
 9 char map[N][N];
10 
11 int abs(int a,int b)
12 {
13     if(a<b) return b-a;
14     else return a-b;
15 }
16 
17 void DFS(int i,int j,int c)
18 {
19     if(flag) return ;
20     if(c>t) return ;    
21     if(i<0||i>=n||j<0||j>=m) {return ;}
22     if(map[i][j]=='D'&&c==t) {flag=ans=true; return ;}
23     int temp=abs(i-end_i)+abs(j-end_j);
24     temp=t-temp-c;
25     if(temp&1) return ;//奇偶剪枝
26 
27     if(!visited[i-1][j]&&map[i-1][j]!='X') 
28     {
29         visited[i-1][j]=true;
30         DFS(i-1,j,c+1);
31         visited[i-1][j]=false;
32     }
33     if(!visited[i+1][j]&&map[i+1][j]!='X') 
34     {
35         visited[i+1][j]=true;
36         DFS(i+1,j,c+1);
37         visited[i+1][j]=false;
38     }
39     if(!visited[i][j-1]&&map[i][j-1]!='X') 
40     {
41         visited[i][j-1]=true;
42         DFS(i,j-1,c+1);
43         visited[i][j-1]=false;
44     }
45     if(!visited[i][j+1]&&map[i][j+1]!='X') 
46     {
47         visited[i][j+1]=true;
48         DFS(i,j+1,c+1);
49         visited[i][j+1]=false;
50     }
51 }
52 
53 int main()
54 {
55     int i,j,x,y,k;
56     while(cin>>m>>n>>t&&(m||n||t))
57     {
58         memset(visited,false,sizeof(visited));
59         k=0;
60         for(i=0;i<n;i++)
61         {
62             for(j=0;j<m;j++)
63             {
64                 cin>>map[i][j];
65                 if(map[i][j]=='S')
66                 {
67                     x=i;y=j;
68                     visited[i][j]=true;
69                 }
70                 if(map[i][j]=='D')
71                 {
72                     end_i=i;end_j=j;
73                 }
74                 if(map[i][j]=='X')k++;
75             }
76         }
77         ans=flag=false;
78         if(n*m-k-1>=t) DFS(x,y,0);
79         if(ans) cout<<"YES"<<endl;
80         else cout<<"NO"<<endl;
81     }
82     return 0;
83 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM