您的位置:首页 > 其它

再谈DFS(1)

2015-08-08 11:00 399 查看


Tempter of the Bone

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

Total Submission(s): 89529 Accepted Submission(s): 24350


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


这是一道经典搜索题,常规方法是深搜,当然要加上必要的剪枝条件

主要的剪枝条件有:

1、剩余可走区域小于时间

2、奇偶性剪枝

3、越界

4、超时等

下面主要说说奇偶性剪枝

若有一迷宫,将迷宫的每一个位置有0或1表示(x+y为偶数时 为0 否则为1):

0 1 0 1 0

1 0 1 0 1

0 1 0 1 0

1 0 1 0 1

从图中我们可以很清晰的看出:任意一个位置周围相邻的必然是与本身值相反的值,也就是说,要想走到与本身值相同的点必然要走偶数步;同理,要想走到与本身相异的值的点必然要走奇数步;


所以,当两个位置的奇偶性相同时(同为0或同为1)若时间为奇数 则必然无法到达;当两个位置的奇偶性不同时(一个为1,另一个为0)若时间为偶数也必不能到达。



本题代码如下(相关解释有注释说明):


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;

int N,M,T,ox,oy,fx,fy,flag=0;
char map[10][10];
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

void DFS(int x,int y,int num)
{
if(x==fx&&y==fy&&num==T)   { flag=1; return ;}//如果找到就返回

if(flag)   {  return ;}

if(num>T)    {  return ;}

int dx,dy;
for(int i=0;i<4;i++)//枚举4个方向
{
map[x][y]='X';//走过的一定记着标记!!!!!
dx=x+dir[i][0];
dy=y+dir[i][1];
if(dx>=0&&dy>=0&&dx<N&&dy<M&&map[dx][dy]!='X')//判断有没有出界和下一步是否能走
{
map[dx][dy]='X';
DFS(dx,dy,num+1);
map[dx][dy]='.';

if(flag) {   return ;}//如果已经找到就不用继续找了
}
}
}

int main()
{
//   freopen("s","r",stdin);

while(cin>>N>>M>>T)
{
int sum=0;flag=0;
if(!N&&!M&&!T)  { break;}

for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')  { ox=i; oy=j; continue;}//标记起始位置
if(map[i][j]=='D')  { fx=i; fy=j; map[i][j]='.'; continue;}//标记终点位置
if(map[i][j]=='X')  { sum++;}
}

if(T>(N*M-sum))
{
cout<<"NO"<<endl;
}else if((ox+oy+fx+fy+T)%2==1)//奇偶性剪枝
{
cout<<"NO"<<endl;
}else
{
DFS(ox,oy,0);//深搜
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: