# hdu1010 Tempter of the Bone

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 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.

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

NO
YES

dfs+剪枝
1. 如果起点到当前点的距离加上该点到终点的最短距离的和与要求最短时间的奇偶性不同，剪掉
2.如果该图可行的点数小于要求的时间，剪掉
3.如果已经找到了该点，并且时间刚好为要求的时间，则以后的搜索全部剪掉
4.如果走的步数已经大于时间并且没有找到终点，剪掉
code：

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#define inf 0x3f3f3f3f
#define LL long long
const int MAX=1e9+7;
const int MAXN=1e5+7;
const double pi=atan(1.0)*4;
using namespace std;
int dirx[10]={0,1,0,-1};
int diry[10]={1,0,-1,0};
int n,m,t;
int flag=0,x2,y2;
int vis[100][100];
char s[100][100];
void dfs(int x,int y,int time)
{
//printf("%d %d\n",x,y);
if(s[x][y]=='D'&&time==t)
{
flag=1;
return ;
}
if(flag==1)
return ;
if(time>t&&s[x][y]!='D')
return ;
int dis=t-abs(x-x2)-abs(y-y2)-time;
if(dis%2||t<0)
return ;
for(int i=0;i<4;i++)
{
int fx=x+dirx[i];
int fy=y+diry[i];
if((s[fx][fy]=='.'||s[fx][fy]=='D')&&vis[fx][fy]==0)
{
vis[fx][fy]=1;
dfs(fx,fy,time+1);
vis[fx][fy]=0;
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
int time=0,x1,y1,xnum=0;
flag=0;
getchar();
memset(vis,0,sizeof(vis));
memset(s,0,sizeof(s));
if(n==0&&m==0&&t==0)
break;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%c",&s[i][j]);
if(s[i][j]=='S')
x1=i,y1=j;
if(s[i][j]=='D')
x2=i,y2=j;
if(s[i][j]=='X')
xnum++;

}
getchar();
}
int p=t%2;
int len=abs(x1-x2)+abs(y1-y2);
if(len>t||len%2!=p)
{
printf("NO\n");
continue;
}
if(n*m-xnum<t)
{
printf("NO\n");
continue;
}
vis[x1][y1]=1;
dfs(x1,y1,time);
if(flag==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0

0 1 0 1 0 1

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120