A - Tempter of the Bone
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
题意:输入行数n和列数m,再输入总数t,判断在t内能否从起始点S走到终点D,‘.’表示可以通过。
思路:抛去奇偶剪枝,其实就是一个简单的dfs,找了些剪枝相关的知识点
那么设所在位置 (x,y) 与 目标位置 (dx,dy)
如果abs(x-y)+abs(dx-dy)为偶数,则说明 abs(x-y) 和 abs(dx-dy)的奇偶性相同,需要走偶数步
如果abs(x-y)+abs(dx-dy)为奇数,那么说明 abs(x-y) 和 abs(dx-dy)的奇偶性不同,需要走奇数步
理解为 abs(si-sj)+abs(di-dj) 的奇偶性就确定了所需要的步数的奇偶性!!
而 (ti-setp)表示剩下还需要走的步数,由于题目要求要在 ti时 恰好到达,那么 (ti-step) 与 abs(x-y)+abs(dx-dy) 的奇偶性必须相同
因此 temp=ti-step-abs(dx-x)-abs(dy-y) 必然为偶数!
上AC代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char str[10][10];
int book[10][10];
int find = 0;
int end_i,end_j,start_i,start_j;
int n,m,t;
void dfs(int x,int y,int step)
{
int tx,ty,i;
int f[4][2]={0,1,-1,0,0,-1,1,0};
i = t-step-abs(x-end_i)-abs(y-end_j);//剪枝部分
if( i%2 == 1 ||i <0)
return;
if(x == end_i&&y == end_j&&t==step)
{
find =1;
return;
}
for(i = 0; i < 4; i++)
{
tx = x + f[i][0];
ty = y + f[i][1];
if(str[tx][ty]=='X'||book[tx][ty] == 1||tx<0||tx>n-1||ty<0||ty>m-1)
continue;
if(str[tx][ty] != 'X')
{
book[tx][ty] = 1;
dfs(tx,ty,step+1);
book[tx][ty] = 0;
}
if(find)
break;//当满足条件时结束循环,否则tle,亲测。
}
return;
}
int main()
{
int i,j;
while(scanf("%d%d%d",&n,&m,&t),(n+m+t)!=0)
{
find = 0;
memset(book,0,sizeof(book));
for(i = 0; i < n; i ++)
scanf("%s",str[i]);
for(i = 0; i < n; i ++)
{
for(j = 0; j < m; j ++)
{
if(str[i][j]=='S')
{
start_i = i;
start_j = j;
}
if(str[i][j] == 'D')
{
end_i = i;
end_j = j;
}
}
}
book[start_i][start_j] = 1;
dfs(start_i,start_j,0);
if(find)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}