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.
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.
'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 }