DFS深搜 + 奇偶剪枝 HDU-1010

Tempter of the Bone

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



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时间走到。每个点只能走一次。
用dfs每个点走一遍,用flag标记是否有刚好符合条件的点,还需要用到奇偶剪枝。

下面介绍一下奇偶剪枝:
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0 
考虑上面这个图:
从0要到1,至少走1步、3步、5步,就是需要奇数步;
从0到0,则要走偶数步。

于是在如果有限制步数(或时间)那么对于 temp = step-cnt - s1-s2;
step-cnt表示剩余步数或时间T,s1+s2表示到那里的最小步数或时间S(即两条垂直路线),如果S是奇数而T是偶数或者S是偶数而T是奇数,那么肯定无法走到那么点。
接下来:  奇数-偶数 = 奇数
奇数-奇数 = 偶数
偶数-奇数 = 奇数
偶数-偶数 = 偶数
因此可以用两数之差的奇偶性来判定。

下面是这道题AC代码:
#include <iostream>
#include <cstdio>
#include <math.h>
#include <queue>
#include <string.h>
#include <algorithm>

using namespace std;
typedef pair<int, int> P;

int n, m, t, k, sx, sy, gx, gy;
char ch[10][10];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool flag;

void dfs(int x, int y, int k){

        if(flag) return ;
        if(x < 0 || x > n-1 || y < 0 || y > m-1 || flag || t < k) return ;
        int temp = abs(t-k) - abs(x-gx) - abs(y-gy); // 奇偶剪枝
        if(temp % 2 != 0 ) {
            return ;
        }

        if(x == gx && y == gy && k == t) {
            flag = true;
            return ;
        }
        for(int i = 0; i < 4 ; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];

            if(ch[nx][ny] != 'X'){
                ch[nx][ny] = 'X';
                dfs(nx, ny, k+1);
                ch[nx][ny] = '.';
        }
    }
    return ;
}

int main()
{
    while(~scanf("%d%d%d", &n, &m, &t) && (n+m+t)){
        int cnt = 0;
        getchar();
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                scanf("%c", &ch[i][j]);
                if(ch[i][j] == 'S'){
                    sx = i;
                    sy = j;
                }
                if(ch[i][j] == 'D'){
                    gx = i;
                    gy = j;
                }
                if(ch[i][j] == 'X')
                    cnt++;
            }
            getchar();
        }
        flag = false;
        ch[sx][sy] = 'X';
        if(n*m - cnt -1 >= t) dfs(sx, sy, 0);
        if(flag) puts("YES");
        else puts("NO");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值