題解報告:hdu 1010 Tempter of the Bone
阿新 • • 發佈:2018-05-12
multiple integer maps app could not 將他 can gin nor
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
解題思路:註解在代碼裏。這裏直接貼一篇大牛的講解吧!很詳細,也很容易懂。鏈接:hdu1010詳細題解
AC代碼:
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
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.
1 #include<bits/stdc++.h> 2 using namespacestd; 3 //迷宮地圖 4 //X: 墻壁,小狗不能進入 5 //S: 小狗所處的起始位置 6 //D: 迷宮的門 7 //. : 空的方格,表示可以經過的點 8 char maps[8][8]; 9 int n,m,t,di,dj;//n行,m列,t是規定時間內到達,(di,dj):門的位置 10 bool escape;//表示是否逃脫 11 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上、下、左、右(方向數組) 12 void dfs(int si,int sj,int cnt)//表示起始位置為(si,sj),要求在第cnt秒到達門的位置 13 { 14 if(si>n || sj>m || si<=0 || sj<=0)return;//處理越界情況,直接退出 15 if(si==di && sj==dj && cnt==t){escape=true;return;}//到達出口 16 int tmp=(t-cnt)-abs(si-di)-abs(sj-dj);//abs(x-ex)+abs(y-ey)表示現在所在的格子到目標格子的距離(不能走對角線)剪枝的核心代碼 17 if(tmp<0 || tmp&1)return;//t-cnt是實際還需要的步數,將他們做差,如果tmp<0或者tmp為奇數,那就不可能到達! 18 for(int i=0;i<4;i++){//深搜當前方向的每個方向 19 if(maps[si+dir[i][0]][sj+dir[i][1]]!=‘X‘){ 20 maps[si+dir[i][0]][sj+dir[i][1]]=‘X‘;//標記為墻壁,表示不能再走過 21 dfs(si+dir[i][0],sj+dir[i][1],cnt+1);//深搜 22 if(escape)return;//若找到,直接返回 23 maps[si+dir[i][0]][sj+dir[i][1]]=‘.‘;//同時還原本來不是墻但被標記的墻(回溯) 24 } 25 } 26 return; 27 } 28 int main() 29 { 30 int si,sj;//表示起點的坐標 31 while(cin>>n>>m>>t && (m+n+t)){ 32 int wall=0; 33 for(int i=1;i<=n;i++){//從1開始,因為在遍歷該點的四個方向時才不會越界的危險 34 for(int j=1;j<=m;j++){ 35 cin>>maps[i][j]; 36 if(maps[i][j]==‘S‘){si=i;sj=j;}//標記小狗的位置 37 else if(maps[i][j]==‘D‘){di=i;dj=j;}//標記出口的位置 38 else if(maps[i][j]==‘X‘)wall++;//計算墻的數量 39 } 40 }//如果剩下的路徑長度小於所需t步,註意n*m-wall==t表示地圖上可以走的路徑長度比t少1,因為此時只有n*m-wall-1條邊 41 if(n*m-wall<=t){cout<<"NO"<<endl;continue;} 42 escape=false;//標記為false,表示還沒找到 43 maps[si][sj]=‘X‘;//直接標記出發點為墻,表示不能返回 44 dfs(si,sj,0);//從起始位置深搜 45 if(escape)cout<<"YES"<<endl;//逃脫成功 46 else cout<<"NO"<<endl; 47 } 48 return 0; 49 }
題解報告:hdu 1010 Tempter of the Bone