hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 60924 Accepted Submission(s): 16672
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.
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.
'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
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int vis[10][10];
char v[10][10];
int n,m,t;
int start_x,start_y;
int end_x,end_y;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
bool dfs(int x,int y,int step)
{
int i,a,b;
if(v[x][y]=='D'&&step==t)return true;
if(x<0||x>=n||y<0||y>=m)return false;
if(step>=t)return false;//剪枝:当step>=T时还没有找到D点
if((t-step-abs(end_x-x)-abs(end_y-y))%2!=0)return false;//剪枝:比理论上的最短距离多出来的必是偶数,这里就是关键
for(i=0;i<4;i++)
{
a=x+dir[i][0];
b=y+dir[i][1];
if(a>=0&&a<n&&b>=0&&b<m&&v[a][b]!='X'&&!vis[a][b])
{
vis[a][b]=1;
if(dfs(a,b,step+1))
return true;
else
vis[a][b]=0;//注意这里一定要归零(白书讲八皇后问题的时候也提到过这一点)
}
}
return false;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&t))
{
if(n==0&&m==0&&t==0)break;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
scanf("%s",v[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(v[i][j]=='S')
{
start_x=i;
start_y=j;
}
if(v[i][j]=='D')
{
end_x=i;
end_y=j;
}
}
}
vis[start_x][start_y]=1;
if(dfs(start_x,start_y,0))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
本文详细解析了使用深度优先搜索(DFS)结合奇偶剪枝策略解决迷宫问题的算法实现过程。通过巧妙地利用奇偶性进行剪枝操作,有效避免了不必要的搜索路径,显著提高了算法效率。具体步骤包括初始化迷宫地图、定义迷宫入口与出口、实现DFS搜索以及剪枝条件判断等关键环节,最终确定狗狗能否成功逃生。
454

被折叠的 条评论
为什么被折叠?



