题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 125246 Accepted Submission(s): 33812
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
题目分析:输入n行m列,S为起点,D为终点(门),'.'代表可以走,'X'为墙,不可走,要求小狗要在t秒时恰好到达门。
基础dfs+奇偶剪枝(所要走的路径长度和最短路径长度一定同奇偶。故二者之差一定不为奇数。)
注意的问题:
(1)剪枝:时间不可能大于 区域数(n*m)-墙(wall) 。
(2)check函数判断边界条件,在dfs函数中,如果满足条件(t秒恰好到达门)escape赋为1,向4个方向进行搜索,若满足check则将搜过的地方变为墙,时间+1继续搜,最后还要把墙还原成路。
基础dfs+奇偶剪枝(所要走的路径长度和最短路径长度一定同奇偶。故二者之差一定不为奇数。)
注意的问题:
(1)剪枝:时间不可能大于 区域数(n*m)-墙(wall) 。
(2)check函数判断边界条件,在dfs函数中,如果满足条件(t秒恰好到达门)escape赋为1,向4个方向进行搜索,若满足check则将搜过的地方变为墙,时间+1继续搜,最后还要把墙还原成路。
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
#include<iostream>
#include<cmath>
using namespace std;
char mp[10][10];
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int m,n,t,di,dj;
bool escape;
//判断边界条件
bool check(int x,int y)
{
if(x<0||x>=m||y<0||y>=n||mp[x][y]=='X')
return 0;
else
return 1;
}
void dfs(int si,int sj,int cnt)
{
//在t秒时恰好到达门
if(si==di&&sj==dj&&cnt==t)
{
escape=1;
return;
}
//奇偶剪枝
int temp=t-cnt-abs(si-di)-abs(sj-dj);
if(temp<0||temp%2)
{
return;
}
for(int i=0;i<4;i++)
{
int xx=si+dir[i][0];
int yy=sj+dir[i][1];
if(check(xx,yy))
{
mp[xx][yy]='X';
dfs(xx,yy,cnt+1);
if(escape)
{
return;
}
//还原
mp[xx][yy]='.';
}
}
}
int main()
{
int si,sj;
while(cin>>m>>n>>t)
{
if(m==0&&n==0&&t==0)
break;
int wall=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='S')
{
si=i;
sj=j;
}
if(mp[i][j]=='D')
{
di=i;
dj=j;
}
if(mp[i][j]=='X')
{
wall++;
}
}
}
if(t>n*m-wall)
{
cout<<"NO"<<endl;
continue;
}
escape=0; //赋初值
mp[si][sj]='X'; //将起点设为墙
dfs(si,sj,0);
if(escape==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}