您的位置:首页 > 其它

【杭电oj1010 】- Tempter of the Bone(dfs,奇偶剪枝)

2017-08-01 17:56 483 查看
Tempter of the Bone

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 127135 Accepted Submission(s): 34284

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

题意:从S沿‘.’走能否刚好t步到D;这道题需要用到 奇偶剪枝 能够很快筛选出许多不符合题的情况

奇偶剪枝知识

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX=10;
typedef long long LL;
char s[MAX][MAX];
int vis[MAX][MAX];
int m,n,t,sx,sy,dx,dy,ok;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};

void dfs(int x,int y,int nl)
{
if(ok)  return;        //先将返回条件写上
vis[x][y]=1;
for(int i=0;i<4;i++)
{
int xx=x+fx[i],yy=y+fy[i];
if(xx>=0&&yy>=0&&xx<m&&yy<n&&!vis[xx][yy])
{
if(s[xx][yy]=='.')
dfs(xx,yy,nl+1);
else if(s[xx][yy]=='D'&&nl+1==t)
ok=1;
}
}
vis[x][y]=0;   //要重新标记为0,否则会影响之后的
}

int main()
{
while(~scanf("%d%d%d",&m,&n,&t),n)
{
memset(vis,0,sizeof(vis));    //初始化
for(int i=0;i<m;i++)
scanf("%s",&s[i]);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(s[i][j]=='S')
sx=i,sy=j;
else if(s[i][j]=='D')
dx=i,dy=j;
}
ok=0;
if((abs(dx-sx)+abs(dy-sy))%2!=t%2)//判断奇偶性是否一致abs(sx-dx)+abs(sy-dy)-t)&1   abs(dx-sx)+abs(dy-sy))%2!=t%2
{
printf("NO\n");
continue;             //不符合直接进行下一循环,不再向下进行
}
dfs(sx,sy,0);
if(ok) printf("YES\n");
else printf("NO\n");

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  杭电 dfs