Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 127232 Accepted Submission(s): 34312
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
题解:奇偶剪枝,如果走偶数步要求的时间是奇数,或者走奇数步要求的时间是偶数,都明显不可行。
所谓奇偶剪纸指的是,剩余时间为奇数时,剩余步数也要为奇数,剩余时间为偶数时,剩余步数也要为偶数,这样才能满足条件。
达到这两个条件要满足,剩余时间-剩余步数==偶数,如果为奇数那么必定不满足条件,就“剪去”,不用考虑了。
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
int n,m,t,ex,ey,sx,sy;
int flag,wall,res,rx,ry;
int step;
char a[80][80];
int fx[]={1,-1,0,0},fy[]={0,0,1,-1};
void dfs(int x,int y,int step)
{
if(flag)
return ;
if(x==ex&&y==ey&&step==t) //条件刚好,到终点时间刚好用完
{
flag=1;
return ;
}
rx=abs(x-ex); //从当前位置到终点需要横向一定的步数
ry=abs(y-ey); //从当前位置到终点需要纵向移动的步数
res=t-step-rx-ry; //剩余时间==总时间-用去的时间-剩余步数
if(res<0||res%2!=0) //看剩下的时间能能否到达终点,tem&1则是判断其是否偶数,根据LCY的奇
return ; //偶性剪枝可得tem必须是偶数,是奇数则不行
for(int i=0;i<4;i++)
{
int xx=x+fx[i],yy=y+fy[i];
if(xx>=0&&xx<m&&yy>=0&&yy<n&&a[xx][yy]!='X')
{
a[xx][yy]='X'; //走过的地方变为墙
dfs(xx,yy,step+1);
a[xx][yy]='.'; //迷宫还原,以便下次广搜
}
}
}
int main()
{
while(~scanf("%d%d%d",&m,&n,&t))
{
int i,j;
wall=0;
flag=0;
step=0;
if(m==0&&n==0&&t==0)
break;
for(i=0;i<m;i++)
scanf("%s",a[i]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(a[i][j]=='S') //找到起点
sx=i,sy=j;
if(a[i][j]=='D') //找到终点
ex=i,ey=j;
if(a[i][j]=='X') //记录墙的总数
wall++;
}
if(m*n-wall<=t) //临街wall+t+1==m*n,所以正常情况下m*n-wall应该>t
{
printf("NO\n");
continue;
}
a[sx][sy]='X'; //出发点是不可能再走的了,变为墙
dfs(sx,sy,step);
if(flag)
printf("YES\n");
else printf("NO\n");
}
}