您的位置:首页 > 其它

4.3.1--Tempter of the Bone--深搜(优化剪枝)

2017-06-23 23:17 281 查看
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


 
解题思路:个人认为这个题更适合用深搜来做,但困难的是如何剪枝,否则会超时,注意可以通过绕路来控制到达的时间,而绕路是会多走偶数倍的,从而减掉奇数
参考:http://blog.csdn.net/akof1314/article/details/4429658

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int n,m,T,ei,ej;
bool flag;
char a[8][8];
bool ok[8][8];
int dx[4]={0,0,1,-1};
int dy[4
bd55
]={1,-1,0,0};
void dfs(int x,int y,int t)
{
if(flag||t>50) return;          //找到结果或时间超过上限,返回
if(x==ei&&y==ej&&t==T)
{
flag=true;          //到达终点且结果刚好
return;
}
int temp=(T-t)-(abs(x-ei)+abs(y-ej));    //剩余时间-到达终点的最短路径
if(temp<0||temp%2==1) return;          //时间不够到终点或为奇数剪枝
for(int k=0;k<4;k++)
{
int xx=x+dx[k],yy=y+dy[k];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
{
if(a[xx][yy]!='X'&&t<T)
{
a[xx][yy]='X';
dfs(xx,yy,t+1);
a[xx][yy]='.';
}
}
}
}
int main()
{
int i,j,bi,bj,wall;
while(scanf("%d%d%d",&n,&m,&T)&&(n+m+T))
{
wall=0;
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
for(j=0;j<m;j++)
{
if(a[i][j]=='S') {bi=i; bj=j;}
else if(a[i][j]=='D') {ei=i; ej=j;}
else if(a[i][j]=='X') wall++;
}
}
if((n*m-wall)<T) {printf("NO\n"); continue;}
flag=false;
a[bi][bj]='X';
dfs(bi,bj,0);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: