现在的位置: 首页 > 综合 > 正文

HDOJ–1010–Tempter of the Bone【DFS+剪枝】

2018年04月24日 ⁄ 综合 ⁄ 共 2395字 ⁄ 字号 评论关闭
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
 


Source
 


Recommend
JGShining
 


思路:我原以为这就是个赤裸裸的DFS,提交之后TLE了,在网上查说要用奇偶剪枝,在dfs函数里加了几个剪枝条件,再提交居然变成了WA。。。最后添加了可走格子数小于规定步数时输出NO的条件才AC

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
char map[10][10];
int n,m,t,k,sx,sy,ex,ey,wall;
int dir[4][2]={1,0,-1,0,0,1,0,-1};

void dfs(int x,int y,int cnt)
{
    int temp,xx,yy,i;
    if(k==1)    return ;            //若满足条件,一直回溯
    if(cnt>t)   return ;            //步数超限,回溯
    if(x==ex&&y==ey&&cnt==t)            //达到要求,让标识符k变为1
    {
        k=1;
        return ;
    }
    temp=t-cnt-(abs(ex-x)+abs(ey-y));       //cnt+abs()=起点到终点最短路径+一个偶数,则temp(要多走的路)为偶数才可能到达(奇偶剪枝)
    if(temp<0||temp%2)  return ;                //奇偶剪枝
    for(i=0;i<4;i++)
    {
        xx=x+dir[i][0];
        yy=y+dir[i][1];
        if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]!='X')
        {
            map[xx][yy]='X';
            dfs(xx,yy,cnt+1);
            map[xx][yy]='.';
        }
    }
}
int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&m,&t),n+m+t)
    {
        wall=0;
        memset(map,0,sizeof(map));
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='X')  wall++;
                else if(map[i][j]=='S')
                {
                    sx=i;
                    sy=j;
                    map[i][j]='X';
                }
                else if(map[i][j]=='D')
                {
                    ex=i;
                    ey=j;
                }
            }
        k=0;
        dfs(sx,sy,0);
        if(k==0||n*m-wall<=t)    printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

抱歉!评论已关闭.