您的位置:首页 > 其它

HDU - 1010 Tempter of the Bone 深搜模板题(DFS)解题报告

2015-07-29 22:26 375 查看


Tempter of the Bone

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

Total Submission(s): 88587 Accepted Submission(s): 24116



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


Author

ZHANG, Zheng

题意:

给你一个N行M列的迷宫,X代表墙,"."代表路,S代表起点,D代表终点,问能不能刚好在K秒从起点到达终点。

题解:

模板DFS,开一个二维数组表示向上下左右走,开一个oount记录步数,若count>K,还没走到就直接break。需要简单的剪枝,如果步数小于两点间的最短路,则一定走不到。还有就是奇偶性剪枝:若两点间的最短路与K奇偶性不同,则一定走不到。这里可以感性的理解一下。

参考代码:

#include<cmath>
#include<iostream>
using namespace std;
char str[10][10];
int map[10][10],v[4][2]={0,1,0,-1,1,0,-1,0};
int n,m,k,i,j,flag,ax,ay,bx,by;
void dfs(int x,int y,int count)
{
int mx,my;
if(bx==x&&by==y)
{
if(k==count)
flag=1;
return;
}
if(count>=k)
return;
if(str[x][y]!='X')
{
for(int i=0;i<4;i++)
{
mx=x+v[i][0];
my=y+v[i][1];
if(mx>=1&&mx<=n&&my>=1&&my<=m&&!map[mx][my]&&str[mx][my]!='X')
{
map[mx][my]=1;
dfs(mx,my,count+1);
map[mx][my]=0;
if(flag)
return;
}
}
}
}
int main()
{

while(scanf("%d%d%d",&n,&m,&k)!=EOF&&(n+m+k))
{
memset(map,0,sizeof(map));
int count;
for(i=1;i<=n;i++)
{
getchar();
for(j=1;j<=m;j++)
{
scanf("%c",&str[i][j]);
if(str[i][j]=='S')
{
ax=i;
ay=j;
}
if(str[i][j]=='D')
{
bx=i;
by=j;
}
}
}
getchar();
if(abs(bx-ax)+abs(by-ay)>k||(ax+bx+ay+by+k)%2==1)
printf("NO\n");
else
{
flag=0;
count=0;
map[ax][ay]=1;
dfs(ax,ay,count);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: