Tempter of the Bone

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 101252    Accepted Submission(s): 27442


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 <malloc.h>
#include <cstring>
#include <string>
#include <stdio.h>
using namespace std;
char map[51][51];
int book[51][51],dist[4][2]= {{0,1},{1,0},{0,-1},{-1,0}},minstep=9999,n,m;
int ex,ey,ok,time;
void dfs(int x,int y,int step)
{
    if(ok == 1)
        return;
    if(x==ex&&y==ey&&step==time)
    {
        ok=1;
        return;
    }
    int mindis=abs(x-ex)+abs(y-ey);  /*当前点到终点的最短距离*/
    if(mindis>time-step||(mindis+ time-step )%2!=0)
        return;
    int tx,ty,k;
    for(k=0; k<=3; k++)
    {
        tx=x+dist[k][0];
        ty=y+dist[k][1];
        if(tx<0||tx>=n||ty<0||ty>=m)
        {
            continue;
        }
        if(((map[tx][ty]=='.')||(map[tx][ty]=='D'))&&book[tx][ty]==0)
        {
            book[tx][ty]=1;
            dfs(tx,ty,step+1);
            book[tx][ty]=0;
        }
    }
    return ;
}
int main()
{

    int startx,starty;
    while(cin>>n>>m>>time&&n&&m&&time)
    {
        int num=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='S')
                {
                    startx=i;
                    starty=j;
                }
                if(map[i][j]=='X')
                {
                    num++;
                }
                if(map[i][j]=='D')
                {
                    ex=i;
                    ey=j;
                }
            }
        }
        if(n * m - num - 1 < time)
        {
            printf("NO\n");
            continue;
        }
        ok = 0;
        memset(book,0,sizeof(book));
        book[startx][starty]=1;
        dfs(startx,starty,0);
        if(ok)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}

你可能感兴趣的:(Tempter of the Bone)