Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55541 Accepted Submission(s): 14983
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.
'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.
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
YES
题目大意:
要求每个点只能走一次,从起点走到终点刚好sum步,点表示可以走得路经。S,D分别代表起点终点。
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
char a[10][10];
int visi[10][10];
int n,m,sum,flag;
struct node
{
int x;
int y;
};
node sta,en; //起点与终点
int dir[4][2]= //四个方向
{
{-1,0},{1,0},{0,-1},{0,1}
}; void dfs(int cx,int cy,int step)
{
int i;
if(flag==1) return; //找到解
if(step==sum)
{
if(cx==en.x&&cy==en.y)
flag=1;
return;
}
int need=abs(cx-en.x)+abs(cy-en.y);
int tmp=sum-step;
if((tmp%2==0&&need%2==1)||(need%2==0&&tmp%2==1)||tmp<need) //奇偶剪枝
return;
for(i=0;i<4;i++)
{
int px,py;
px=cx+dir[i][0],py=cy+dir[i][1];
if(px>=0&&px<n&&py>=0&&py<m&&(a[px][py]=='.'||a[px][py]=='D')&&!visi[px][py])
{
visi[px][py]=1;
dfs(px,py,step+1);
visi[px][py]=0; //回溯
}
}
//return;
} int main()
{
int i,j;
while(scanf("%d%d%d",&n,&m,&sum))
{
if(n==0&&m==0&&sum==0)
break;
for(i=0;i<n;i++)
scanf("%s",a[i]);
flag=0;
for(i=0;i<n;i++) //找起点
{
for(j=0;j<m;j++)
{
if(a[i][j]=='S')
{
sta.x=i; sta.y=j;
flag=1; break;
}
}
if(flag) break;
}
flag=0;
for(i=0;i<n;i++) //找终点
{
for(j=0;j<m;j++)
{
if(a[i][j]=='D')
{
en.x=i; en.y=j;
flag=1; break;
}
}
if(flag) break;
} flag=0;
memset(visi,0,sizeof(visi));
visi[sta.x][sta.y]=1;
dfs(sta.x,sta.y,0);
if(flag) puts("YES");
else puts("NO");
}
return 0;
} //703MS 228K