杭电ACM 1010 搜索题

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.


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

主要的剪枝条件有:

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)若时间为偶数也必不能到达。


用代码表示的方法很多,这里贴出我的做法:

          if((endi+starti+startj+endj+t)%2==1)//odd and even ( prune)
          {
              cout<<"NO"<<endl;
              continue;
          }

在写递归搜索时普遍的做法是 先写出退出条件然后在写自身递归,不多说了,直接上代码

#include <iostream>
#include <math.h>
using namespace std;

char maze[9][9];
int starti,startj,endi,endj,t,n,m;
int flag;
int direction[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
             

void DFS(int px,int py,int time)
{
    int i;
    if(flag==1)return;
    if(maze[px][py]=='X')return;
    if(px==endi&&py==endj&&time==t){flag=1;return;} //find
    if(px<1||px>n||py<1||py>m)return; //out of range
    if(time>t)return;
    
    for(i=0;i<4;i++)
    {
        maze[px][py]='X';
        DFS(px+direction[i][0],py+direction[i][1],time+1);
        maze[px][py]='.';
        if(flag==1)return;
    }
    
}

int main()
{
    int i,j,block;
    char temp;
    while(cin>>n>>m>>t)
    {
        block=0;
        flag=0;
        if(n==0&&m==0&&t==0)break;
       
        for(i=1;i<=n;i++)
        {
          for(j=1;j<=m;j++)
          {
              //scanf("%c",&maze[i][j]);
              cin>>maze[i][j];
            
              if(maze[i][j]=='S'){starti=i;startj=j;continue;}
              if(maze[i][j]=='X'){block++;continue;}
              if(maze[i][j]=='D'){endi=i;endj=j;}
          }
          //scanf("%c",&temp);
        }
          if(t>(n*m-block)) //if time is more than the place that can move to
          {
              //printf("NO\n");
              cout<<"NO"<<endl;
              continue;
          }
          if((endi+starti+startj+endj+t)%2==1)//odd and even ( prune)
          {
              cout<<"NO"<<endl;
              continue;
          }
          
          DFS(starti,startj,0);
          if(flag==1)cout<<"YES"<<endl;
          else cout<<"NO"<<endl;
          
    }

    return 0;
}

做这题时遇到了好多麻烦,这里写几点注意事项:

1、这题的输入输出最好用c++的,因为测试代码有可能不是分为多行,而且用c的时候换行符也可能引起错误

2、写剪枝条件的时候一定要细心(我就是因为越界条件写错了而WA了好多次。。。)


最后给出一些测试数据:

1 5 4

S...D

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
2 2 1
S.
.D
8 8 7
.DXS...X
........
XX..XX..
.X.X.X.X
..X.....
X....X..
........
XXXX....
5 4 18
S...
....
....
....
...D
6 6 10
S.....
......
......
......
......
.....D
2 3 2
SDX
..X
2 2 3
SD
..
2 2 2
SD
XX
4 4 6
.S..
XXX.
XXX.
XXXD
5 4 8
S...
.XX.
.X..
.X.X
....
3 3 3333
.S.
...
...
2 2 1
SD
..
1 5 4
S...D
4 5 5
.S...
..X..
.XDX.
..X..
2 4 7
SD..
....
2 2 3
S.
D.
4 4 9
S..X
X.X.
..XD
....


输出:

YES
NO
YES
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
YES
YES
NO
YES
YES
YES





你可能感兴趣的:(杭电ACM 1010 搜索题)