Tempter of the Bone
Problem DescriptionThe 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 is a trap, and he tried desperately to get out of this maze.
The maze is a rectangle with sizes N by M. There is a door in the maze. At the beginning, the door is 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 in 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 on one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Inputthe input consists of multiple test cases. The first line of all test case contains three integers n, m, and T (1 < N, m < 7; 0 < T <), which denote The sizes of the maze and the time at which the door would open, respectively. The next N lines give the maze layout, with all 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 is a not-to-be processed.
Outputfor each test case, print on one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input4 4 5s.x ... X... Xd.... 3 4 5 S.x ... X.... D0 0 0
Sample Outputno YES
1#include <iostream>2#include <stdio.h>3#include <stdlib.h>4 using namespacestd;5 6 intb;7 BOOLFlag;8 intstart_x,start_y,end_x,end_y;9 intd[4][2]={0,1,1,0,0,-1,-1,0};Ten Charmap[Ten][Ten]; One A voidDFS (intXintYintt) - { - inti; the if(flag==true)return; - if(T<abs (End_x-x) +abs (end_y-y) | | (T-abs (End_x-x) +abs (end_y-y))%2)return; - Else if(t==0) - { + if(x==end_x&&y==end_y) - { +flag=true; A return; at } - Else return; - } - Else - { - for(i=0;i<4; i++) in { - intnx=x+d[i][0],ny=y+d[i][1]; to if(nx>=0&&nx<a&&ny>=0&&ny<b&& (map[nx][ny]=='.'|| map[nx][ny]=='D')) + { -map[nx][ny]='X'; theDFS (nx,ny,t-1); *map[nx][ny]='.'; $ }Panax Notoginseng } - return; the } + } A the intMain () + { - inti,j,t; $ while(cin>>a>>b>>t&& (a| | b| |t)) $ { - for(i=0; i<a;i++) -Cin>>Map[i]; the for(i=0; i<a;i++) - for(j=0; j<b;j++)Wuyi { the if(map[i][j]=='S') - { Wustart_x=i; -start_y=J; About } $ Else if(map[i][j]=='D') - { -end_x=i; -end_y=J; A } + Else Continue; the } -flag=false; $ DFS (start_x,start_y,t); the if(flag==false) cout<<"NO"<<Endl; the Elsecout<<"YES"<<Endl; the } the return 0; -}
HDU 1010 Temper of the bone (deep Search + pruning)