您的位置:首页 > 其它

【HDU 1010】Tempter of the Bone(DFS,奇偶剪枝)

2017-07-30 12:18 375 查看

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.

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

题意

狗狗要在特定时刻逃出N*M迷宫,迷宫已输入,‘.’表示可以走的路,’#’表示不能走的墙,’S’表示开始位置,‘D’表示结束位置。不能走回头路

思路以及细节点

输入时将起点、终点位置记录,并将墙的vis置为true(不能 再 走了)

DFS,先贴代码

void DFS(int x,int y,int time)
{
//判出口条件
if(x<1||y<1||x>n||y>m) return ;
if(Map[x][y]=='D'&&time==T)
{
judge=true;
//cout<<"(」゜ロ゜)」";
return ;
}
if(((T-time)%2 != (abs(x-endx) + abs(y-endy)) %2)) return;//奇偶剪枝
if(T-time-abs(x-endx)-abs(y-endy)<0) return;//步数不够剪枝
//进行操作
vis[x][y]=true;
for(int i=0;i<4;i++)
{
int tx,ty;
tx=x+dir[i][0];
ty=y+dir[i][1];
if(!vis[tx][ty]&&!judge)
{
DFS(tx,ty,time+1);
vis[tx][ty]=false; //注意是将tx,ty(当前递归变量)回溯,不影响下次循环
}
}
}


出口条件前面已经写过很多了,这里就不讲了。重点在于奇偶剪枝(奇偶剪枝是什么?)以下是比较精简一点的说法:

奇偶剪枝,就是步数的偏移量永远为偶数

因此可以得出:最短路径步数+偏移量(偶数)=某一可行解歩数。

最短路歩数和某一可行解歩数的奇偶性相同。

利用第三条可写出以下代码

if(((T-time)%2 != (abs(x-endx) + abs(y-endy)) %2)) return;


步数不够的时候无需继续递归,也可以剪枝

if(T-time-abs(x-endx)-abs(y-endy)<0) return;


递归调用后记得回溯

vis[tx][ty]=false; //注意是将tx,ty(当前递归变量)回溯,不影响下次循环


代码示例

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//四个方向
char Map[10][10];
bool vis[10][10];
int n,m,T;
bool judge; //判断是否能逃生
int startx,starty;//起点坐标
int endx,endy;//终点坐标

void DFS(int x,int y,int time)
{
//判出口条件
if(x<1||y<
be4d
1||x>n||y>m) return ;
if(Map[x][y]=='D'&&time==T)
{
judge=true;
//cout<<"(」゜ロ゜)」";
return ;
}
if(((T-time)%2 != (abs(x-endx) + abs(y-endy)) %2)) return;//奇偶剪枝
if(T-time-abs(x-endx)-abs(y-endy)<0) return;//步数不够剪枝
//进行操作
vis[x][y]=true;
for(int i=0;i<4;i++)
{
int tx,ty;
tx=x+dir[i][0];
ty=y+dir[i][1];
if(!vis[tx][ty]&&!judge)
{
DFS(tx,ty,time+1);
vis[tx][ty]=false; //注意是将tx,ty(当前递归变量)回溯,不影响下次循环
}
}
}

int main()
{
while(cin>>n>>m>>T&&n&&m&&T)
{
judge=false;
memset(vis,false,sizeof(vis));
memset(Map,0,sizeof(Map));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>Map[i][j];
if(Map[i][j]=='S')  //起点
startx=i,starty=j;
if(Map[i][j]=='X')  //墙不能访问 ,vis改为true
vis[i][j]=true;
if(Map[i][j]=='D')  //终点
endx=i,endy=j;
}
DFS(startx,starty,0);
if(judge)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs HDU