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

HDU 1010 Tempter of the Bone DFS剪枝

2018年01月20日 ⁄ 综合 ⁄ 共 2623字 ⁄ 字号 评论关闭

Tempter of the Bone

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

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
/*
hdu 1010 DFS+剪枝
剪枝就是
abs(x-ex) + abs(y - ey)表示现在所在的格子到目标格子的距离(不能走对角线)
t-cnt是实际还需要的步数,将他们做差
如果temp < 0或者temp为奇数,那就不可能到达!

map的奇偶性以01编号:
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
我们发现从0走一步一定走到1,从1走一步一定走到0。
也就是说,如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。
同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。
 
也就是说,
狗的坐标x、y和,对2取余是它的奇偶性,
Dxy和,对2取余是D的奇偶性。
两个奇偶性一加再对2取余为其奇偶,
拿这个余数去与剩下时间对2取余的余数作比较即可。
奇-奇=偶  偶-偶=偶 
故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;                                                                                       
*/

#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;

char map[10][10];
int flag,n,m,t,sx,sy,ex,ey;
int dir[4][2]={-1,0,1,0,0,1,0,-1};

void DFS(int x,int y,int t)
{
    
    int i,endx,endy,temp;
    if(flag==1) return ;

    temp=t-abs(ex-x)-abs(ey-y);
    if(temp<0||temp&1) return ;
    
    if(t==0)
        {
            if(x==ex&&y==ey)
            {
                flag=1;return ;
            }
            else 
                return ;
        }
    for(i=0;i<4;i++)
    {
        endx=x+dir[i][0];
        endy=y+dir[i][1];
        if (endx>=0&&endx<n&&endy>=0&&endy<m&&(map[endx][endy]=='.'||map[endx][endy]=='D'))
        {
            map[endx][endy]='X';
            DFS(endx,endy,t-1);
            map[endx][endy]='.';
        } 
        
    }
    return ;        
}

int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&m,&t)&&(n+m+t))
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='S')
                {
                    sx=i;sy=j;
                }
                if(map[i][j]=='D')
                {
                    ex=i;ey=j;
                }
            }
        }
        flag=0;
        DFS(sx,sy,t);
        if(flag==0) 
            printf("NO\n");
        else 
            printf("YES\n");
    } 
    return 0;
} 

抱歉!评论已关闭.