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;
反之亦反。
解題思路:這道題,要用到剪枝搜索來做,否則會超時。剪掉的條件是,如果可走地板數目小於給定的時間,絕對不可能得救。還有就是狗走到門的時間必須和題目給定的時間是同奇同偶的,否則也不能在指定的那秒到達門,也不可能得救,剪掉這兩種情況後。就用DFS來做。從起點出發,DFS周圍的路,走過的路就標記為不可走,一只搜索下去,如果是一條不歸路(就是活不了啦)就回溯,把可能的路都搜索一遍過去,看看是否有可行方案。
源碼:(TLE)——未剪枝、未加優化
#include<string.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std; int n,m,t; char map[10][10]; int flag; int di,dj,wall; int i,j,si,sj; int to[4][2] = {-1,0,1,0,0,-1,0,1}; void dfs(int si,int sj,int cnt)//深搜 { if(si>=n || sj>=m || si<0 || sj < 0)//出界 return ; if(cnt == t && si == di && sj == dj)//到達終點 flag = 1; if(flag) return ; for(int i = 0; i<4; i++) { int nextx=si+to[i][0]; int nexty=sj+to[i][1]; if(nextx>=0&&nextx<m&&nexty>=0&&nexty<n&&map[nextx][nexty]!='X') { map[nextx][nexty]='X';//走過的地方變為牆 dfs(nextx,nexty,cnt+1); map[nextx][nexty]='.';//迷宮還原,以便下次廣搜 } } return ; } int main() { while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0)) { getchar(); wall = 0; for(i = 0; i<n; i++) for(j = 0; j<m; j++) { cin>>map[i][j]; if(map[i][j] == 'S') { si = i; sj = j; //標記起點 } else if(map[i][j] == 'D') { di = i; dj = j; //標記終點 } else if(map[i][j] == 'X') wall++;//對“#計數” } flag = 0; map[si][sj] = 'X';//出發點是不可能再走的了,變為牆 dfs(si,sj,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; } #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std; int n,m,t; char map[10][10]; int flag; int di,dj,wall; int i,j,si,sj; int to[4][2] = {-1,0,1,0,0,-1,0,1}; void dfs(int si,int sj,int cnt)//深搜 { if(si>=n || sj>=m || si<0 || sj < 0)//出界 return ; if(cnt == t && si == di && sj == dj)//到達終點 flag = 1; if(flag) return ; for(int i = 0; i<4; i++) { int nextx=si+to[i][0]; int nexty=sj+to[i][1]; if(nextx>=0&&nextx<m&&nexty>=0&&nexty<n&&map[nextx][nexty]!='X') { map[nextx][nexty]='X';//走過的地方變為牆 dfs(nextx,nexty,cnt+1); map[nextx][nexty]='.';//迷宮還原,以便下次廣搜 } } return ; } int main() { while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0)) { getchar(); wall = 0; for(i = 0; i<n; i++) for(j = 0; j<m; j++) { cin>>map[i][j]; if(map[i][j] == 'S') { si = i; sj = j; //標記起點 } else if(map[i][j] == 'D') { di = i; dj = j; //標記終點 } else if(map[i][j] == 'X') wall++;//對“#計數” } flag = 0; map[si][sj] = 'X';//出發點是不可能再走的了,變為牆 dfs(si,sj,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std; int n,m,t; char map[10][10]; int flag; int di,dj,wall; int i,j,si,sj; int to[4][2] = {-1,0,1,0,0,-1,0,1}; void dfs(int si,int sj,int cnt)//深搜 { if(si>=n || sj>=m || si<0 || sj < 0)//出界 return ; if(cnt == t && si == di && sj == dj)//到達終點 flag = 1; if(flag) return ; for(int i = 0; i<4; i++) { int nextx=si+to[i][0]; int nexty=sj+to[i][1]; if(nextx>=0&&nextx<m&&nexty>=0&&nexty<n&&map[nextx][nexty]!='X') { map[nextx][nexty]='X';//走過的地方變為牆 dfs(nextx,nexty,cnt+1); map[nextx][nexty]='.';//迷宮還原,以便下次廣搜 } } return ; } int main() { while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0)) { getchar(); wall = 0; for(i = 0; i<n; i++) for(j = 0; j<m; j++) { cin>>map[i][j]; if(map[i][j] == 'S') { si = i; sj = j; //標記起點 } else if(map[i][j] == 'D') { di = i; dj = j; //標記終點 } else if(map[i][j] == 'X') wall++;//對“#計數” } if(n*m-wall<=t||abs(si-di)+abs(sj-dj)>t)//t是代表要走的步數,步數加牆數必須小於總格子數的,因為所有格子中還包括了S和D,這是剪枝 { printf("NO\n"); continue; //abs(si-ei)+abs(sj-ej)為起點到終點的最短步數 } if((abs(si-di)+abs(sj-dj))%2!=(t%2)) { //狗走到門的時間必須和題目給定的時間是同奇同偶的,否則也不能在指定的那秒到達門 printf("NO\n"); continue; } flag = 0; map[si][sj] = 'X';//出發點是不可能再走的了,變為牆 dfs(si,sj,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }