hdu1010 dfs+奇偶剪枝

Tempter of the Bone

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


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
 
题目意思是一只狗走迷宫,S开始位置,D为出口,给一个时间t,只能在t秒是出去,且每一块都只能站一秒,问能不能正好出去
以为只是一个模板题,交了一遍就超时了,查了一下发现要奇偶剪枝。
剪枝方法:离终点的最低步数和剩下的时间的奇偶性相同时才可能到达,奇偶性不同直接排除这条路
代码如下:
#include<stdio.h>
#include<string.h>

char map[10][10];
int sx,sy,dx,dy;
int flag,visit[10][10];
int n,m,d;
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};
int abs(int a)
{
    return a>=0?a:-a;
}
void dfs(int x,int y,int sum)
{
    if(flag==1)//已经找到了返回 
        return ;
    if(x==dx&&dy==y&&sum==d)//找到标记 
    {
        flag=1;
        return ;
    }
    int min=abs(dx-x)+abs(dy-y);//现在距离终点最短时间 
    if(min>d-sum||(min+d-sum)%2!=0)//主要是这步剪枝   若最短时间都能到,或奇偶性不同 
        return ;
    for(int i=0;i<4;i++)
    {
        int xx,yy;
        xx=x+dir[i][0];
        yy=y+dir[i][1];
        if(xx>=0&&xx<n&&yy>=0&&yy<m&&!visit[xx][yy]&&map[xx][yy]!='X')//下一步可以搜索 
        {
            visit[xx][yy]=1;//标记搜索过 
            dfs(xx,yy,sum+1); 
            visit[xx][yy]=0;//回溯 
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&d)!=EOF)
    {
        if(n==0&&m==0&&d==0)
            break;
        for(int i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(int j=0;j<m;j++)
            {
                if(map[i][j]=='S')//标记起点 
                {
                    sx=i;
                    sy=j;
                }
                else if(map[i][j]=='D')//终点 
                {
                    dx=i;
                    dy=j;
                }
            }
        }
        flag=0;
        memset(visit,0,sizeof(visit));
        visit[sx][sy]=1;
        dfs(sx,sy,0);
        if(flag==1)
            printf("YES
");
        else
            printf("NO
");
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/xiongtao/p/9107624.html