Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 107818 Accepted Submission(s): 29298
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
题意:小狗从S开始到D,. 为路, X为墙,D为终点。在 t 时间 到达出口
思路:直接深搜,然后就挂了。。。
关键剪枝:在main函数中,(1)将可走路数与给的 t 进行比较 t > 路数 排除
(2)判断奇偶剪枝
奇偶剪枝:当有障碍物的迷宫时,t = 最短路径 + 一个偶数
因为:奇(t) = 奇(最短路径) + 偶数
偶(t) = 偶(最短路径) + 偶数
而:最短路径为:Dx - Sx + Dy - Sy
所以在判断 (t - (Dx - Sx + Dy - Sy ))% 2 == 1 排除
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char maze[10][10];
int n,m,t,flag,direction[][2] = {1,0 , 0,1 , -1,0 , 0,-1};
void dfs(int x,int y,int fx,int fy,int steps)
{
int i;
if(steps > t)
return;
if(x == fx && y == fy && steps < t)
return;
if(x == fx && y == fy && steps == t)
{
flag = 1;
return;
}
for(i=0;i<4;i++)
{
if(flag)
break;
int tx = x + direction[i][0] , ty = y + direction[i][1];
if(maze[tx][ty] == '.' && tx >= 0 && tx < n && ty >= 0 && ty < m && steps < t)
{
maze[tx][ty] = 'X';
dfs(tx,ty,fx,fy,steps+1);
maze[tx][ty] = '.';
}
}
return;}
int main()
{
int i,j,sx,sy,ex,ey;
while(scanf("%d%d%d",&n,&m,&t)!=EOF && (n || m || t))
{
getchar();
for(i=0;i<n;i++)
scanf("%s",maze[i]);
int cot = 1;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(maze[i][j] == '.')
cot++;
if(maze[i][j] == 'S')
{
sx = i;
sy = j;
}
if(maze[i][j] == 'D')
{
ex = i;
ey = j;
}
}
}
maze[ex][ey] = '.';
if(t > cot || (t - (ex - sx + ey - sy)) % 2)//奇偶剪枝,s到e的最短路径为 (ex - sx + ey - sy) 而t = 最短路径+偶数 ;因为 奇(最短路)+ 偶 = 偶(t);偶(最短路)+偶 = 偶(t)
{
printf("NO\n");
continue;
}
flag = 0;
dfs(sx,sy,ex,ey,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}