Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 128878 Accepted Submission(s): 34798
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
Conclude:
1.DFS奇偶性剪枝,否则超时;
2.多字符串输入只要有空格或者换行,就不能用scanf %c;
3.内外两个回溯;
#include<iostream> #include<cstring> using namespace std; char a[7][7]; int vis[7][7]; //标记能走的位置(除去出界的位置,走过的位置和不能走的位置) int n,m,t; //如此就不在递归中判断出界 int x1,y1; int x,y; bool flag; int b[4]={1,-1,0,0}; int c[4]={0,0,1,-1}; int abs(int n) //发现如果包含cmath头文件中 ,x0,y0,x1,y1等会产生歧义 { if(n<0) n=-1*n; return n; } void dp(int k) { // if(x1<0||x1>n||y1<0||y1>m) // return; if(flag==true) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! return; //!!!!要点:只要有一种 符合条件,就可以结束所有的递归!!!! if((abs(x-x1)+abs(y-y1))%2!=(t-k)%2) return; //!!!!要点:由奇偶性剪枝,否则超时 !!!! if(abs(x-x1)+abs(y-y1)>t-k) return; //减时操作:最短路径的时间不能超过剩余时间 if(k==t) { if(a[x1][y1]==‘D‘) flag=true; return; } vis[x1][y1]=0; for(int i=0;i<4;i++) { if(vis[x1+b[i]][y1+c[i]]) { x1+=b[i];y1+=c[i]; dp(k+1); x1-=b[i];y1-=c[i]; } // for循环内回溯,防止对另一种走法产生干扰。 } vis[x1][y1]=1; // 外部标记回溯 } int main() { while(cin>>n>>m>>t) { int sum=0; flag=false; if(n==0&&m==0&&t==0) break; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { scanf("%s",a[i]); //逐行输入,无&,千万不能scanf( "%c",&a[i][j]) } //scanf %c 能读取空格和换行符。 for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]==‘S‘) { x1=i; y1=j; } if(a[i][j]==‘D‘) { x=i; y=j; vis[i][j]=1; sum++; } if(a[i][j]==‘.‘) { vis[i][j]=1; sum++; } } } if(sum<t) //减时操作:可走方格不能少于固定时间 { printf("NO\n"); continue; } dp(0); if(flag==true) printf("YES\n"); else printf("NO\n"); } return 0; }