Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 54236 Accepted Submission(s): 14606
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
Sample Output
NO YES
Code:
S开始的地方深搜前一定标志1,载坑里了==
百科上的人家总结的奇偶剪枝原理讲的挺好的~
#include"stdio.h"
#include"string.h"
int sign[10][10];
int sx,sy,ex,ey,m,n,t,flag;
int abs(int a)
{
if(a<0) return -a;
return a;
}
void dfs(int yu,int x,int y)//还需要yu步到达door处
{
// printf("x:%d y:%d nn:%d\n",x,y,nn);
if(flag==1)return;
if((abs(x-ex)+abs(y-ey))>yu || (yu-abs(x-ex)-abs(y-ey))%2) return;//奇偶剪枝
//该点dao终点需要奇数步则该点不可用
if(yu==0)
{
if(x==ex && y==ey){flag=1;return;} //如果在要求时间内正确到达,返回1
else return;
}
if(!sign[x][y+1] && y+1<=n)//四个方向
{
sign[x][y+1] = 1;
dfs(yu-1,x,y+1);
sign[x][y+1] = 0;
}
if(!sign[x+1][y] && x+1<=m)
{
sign[x+1][y] = 1;
dfs(yu-1,x+1,y);
sign[x+1][y] = 0;
}
if(!sign[x][y-1] && y-1>0)
{
sign[x][y-1] = 1;
dfs(yu-1,x,y-1);
sign[x][y-1] = 0;
}
if(!sign[x-1][y] && x-1>0)
{
sign[x-1][y] = 1;
dfs(yu-1,x-1,y);
sign[x-1][y] = 0;
}
return;
}
int main()
{
int i,j;
char str[10];
while(scanf("%d%d%d",&m,&n,&t),m&&n&&t)
{
memset(sign,0,sizeof(sign));
for(i=1;i<=m;i++)
{
scanf("%s",str);
for(j=1;j<=strlen(str);j++)//标记标记位并记录开始和结束坐标
{
if(str[j-1] == 'X') sign[i][j] = 1;
if(str[j-1] == 'S') {sx = i;sy = j;}
if(str[j-1] == 'D') {ex = i;ey = j;}
}
}
flag=0;
sign[sx][sy]=1;//!!!一定改变起始标志位啊!!
dfs(t,sx,sy);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}