Tempter of the Bone

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 1
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
 
Author
ZHANG, Zheng
 
Source
ZJCPC2004
 
在T时刻,到达。
 Tempter of the Bone     搜索---奇偶性剪枝-LMLPHP
 
 
详细的看代码。开始做的时候,一直超时。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std; int n,m,T,sx,sy,dx,dy;
char a[][];
bool vis[][],Glag;
int map1[][]={ {,},{,},{-,},{,-} }; void dfs(int x,int y,int cur)
{
int i,x1,y1;
if(cur==T && x==dx && y==dy)
{
Glag=true;
return;
}
if(Glag==true) return;
int t,s;
t=T-cur;
s=(int)abs(dx-x)+(int)abs(dy-y);//剩余的时间 t - 剩余的步数 s
//如果,t是奇数,s是奇数。那么t-s是偶数。
//如果,t是偶数,s是奇数。那么t-s是奇数。
//所以只要判断 相减后是否>0 && 偶数。
t=t-s;
if(t<||(t&)==) return;//奇偶性剪枝 for(i=;i<;i++)
{
x1=x+map1[i][];
y1=y+map1[i][];
if(x1>=&&x1<=n && y1>=&&y1<=m && vis[x1][y1]==false && a[x1][y1]!='X')
{
vis[x1][y1]=true;
dfs(x1,y1,cur+);
vis[x1][y1]=false;
}
}
} int main()
{
int i,j,wall;
while(scanf("%d%d%d",&n,&m,&T)>)
{
if(n==&&m==&&T==)break;
for(i=;i<=n;i++)
scanf("%s",a[i]+); wall=;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
if(a[i][j]=='S')
{
sx=i;
sy=j;
}
else if(a[i][j]=='D')
{
dx=i;
dy=j;
}
else if(a[i][j]=='X')
wall++; if(n*m--wall<T)//这也是一个优化,T太大了,根本走不到。
{
printf("NO\n");
continue;
}
Glag=false;
memset(vis,false,sizeof(vis));
vis[sx][sy]=true;
dfs(sx,sy,);
if(Glag==true)
printf("YES\n");
else printf("NO\n");
}
return ;
}
 
 
05-11 17:27