您的位置:首页 > 其它

HDOJ 1010 Temper of the Bone

2016-03-21 20:21 344 查看
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//
//  main.cpp
//  Tempter of the Bone
//
//  Created by 张嘉韬 on 16/3/21.
//  Copyright © 2016年 张嘉韬. All rights reserved.
//

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int n,m,T,map[10][10],flag,ex,ey;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
void print(int k,int d)
{
cout<<k<<" "<<d<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<map[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
int safe(int x,int y)
{
int flag=1;
if(x<=0||x>n||y<=0||y>m||map[x][y]==0) flag=0;
return flag;
}
void dfs(int k,int x,int y)
{
if(flag==1||k>T) return ;
int tempx,tempy;
for(int i=0;i<4;i++)
{
tempx=x+dx[i];
tempy=y+dy[i];
if(k==1)
{
//cout<<"*"<<i<<" "<<safe(tempx,tempy)<<endl;
}
if(safe(tempx,tempy)==1)
{
map[tempx][tempy]=0;
//print(k,i);
if(k==T&&tempx==ex&&tempy==ey)
{
//print(k,i);
flag=1;
return;
}
else dfs(k+1,tempx,tempy);
map[tempx][tempy]=1;
}
}
}
int main(int argc, const char * argv[]) {
//freopen("/Users/zhangjiatao/Desktop/input.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&T)==3)
{
int sx,sy;
flag=0;
if(n==0) break;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
char temp;
cin>>temp;
if(temp=='S')
{
map[i][j]=0;
sx=i;
sy=j;
}
else if(temp=='X') map[i][j]=0;
else if(temp=='D')
{
map[i][j]=1;
ex=i;
ey=j;
}
else map[i][j]=1;
}
}
int dis;
dis=abs(ex-sx)+abs(ey-sy);
if(dis%2!=T%2)
{
cout<<"NO"<<endl;
continue;
}
//print(-1,-1);
dfs(1,sx,sy);//NO
if(flag==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

Debug总结

1.问题出现在了状态没有进行还原,导致改变方向再次搜索的时候因为之前的搜索改变的状态使要改变的新方向的map状态被占用,一定要好好考虑状态还原这一部分。

奇偶剪枝

1.奇偶剪枝的道理很简单,定义dis是dis=abs(ex-sx)+abs(ey-sy);那么在奇数时间之内只能移动到奇数格子,偶数时间内只能移动到偶数格子,这个方法可以应用到时间给定的搜索中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: