hdu 1010 Tempter of the Bone

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


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代表门, X代表墙, . 带代表是空白的。 走过的路是会在你离开后陷下去的。 门是在第T时刻才出现, 早到,和迟到, 都门都会小时。
问小狗是否能到达门。

思路 :深搜。   首先先别考虑时间问题。  每一个递归表明一部,  每次都递归周围4个格子。 并且将 当前坐标, 和剩下步数传给下一dfs(x, y, num - 1); 
     每次递归完就要回溯。 找到一条可行路径就标记为1 并退出。 否则知道递归完才退出。
     考虑时间。   按照上面的递归, 每个格子 4次递归。就算是 6 * 6 的格子也要  4e36次方   就会超时。
     所以就要剪枝了:  步数剪枝:  
首先要粗略的判断是否可行。  如果所有空白的地方 都小于等于T, 那么绝对没机会看到门的出现了,不进入递归。
语句 if( N * M - wall <= T)
精细点的判断。
进入递归后,  如果   剩余步数  小于   当前的坐标到门的坐标的最少步数(即没障碍物), 那么当前坐标是无法到达门的, 也不继续递归周围4个。
语句 if(num < abs(index_i - x) + abs(index_j  - y)) return ;
接着是 奇偶剪枝: 
 如果开始坐标到门的坐标的最少步数的奇偶性 与 剩余步数的 奇偶性不一样, 那也是无法到达目的。
因为你无论怎么换路径都是在 + 2 * K; 奇偶性是不会变化的。
语句 if( (T + abs(start_i - index_i) + abs(start_j  - index_j)) % 2)
如图是   5 * 7 的迷宫,S 到 D 是一定要走偶数步的,   如果T 是 奇数 那无法看到门的出现, 除非把D改为 1出现的地方。
还有:  在超出边界的, 就无需在递归。
经过两次剪枝和判断边界后,  就省去了不必要的递归。递归是很耗时间的。


   
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max_size 10
char ch[max_size][max_size];
int start_i, start_j, index_i, index_j, N, M, T, biaoji;
int d[4][2]={0,1,1,0,0,-1,-1,0};
void dfs(int x, int y, int num) {
    if(biaoji)
        return;
    if(!num) {
        if(x == index_i && y == index_j)
            biaoji = 1;
        return;
    }
    if(num < abs(index_i - x) + abs(index_j - y)) //精细步数剪枝
        return ;
    for(int i = 0; i < 4; i++) {  
        int nx = x + d[i][0],ny = y + d[i][1];  
        if(!ch[nx][ny])    {//边界检查
            ch[nx][ny] = 1;             
            dfs(nx, ny, num - 1);  
            ch[nx][ny] = 0;             
        }   
    }   
    return ;
}
int main() {
    char str[10];
    while(scanf("%d%d%d", &N, &M, &T) && N || M || T) {
        int wall = 0;
        memset(ch, 1, sizeof(ch));
        for (int i = 1; i <= N; i++)  {  
            scanf("%s", str);
            for (int j = 1;j <= M; j++)  {  
                ch[i][j] = str[j - 1];     
                if(ch[i][j] == 'S')
                    start_i = i,start_j = j,ch[i][j] = 1;
                else if(ch[i][j] == 'D') 
					index_i = i, index_j = j, ch[i][j] = 0;
                else if(ch[i][j] == 'X') 
					wall++,ch[i][j] = 1;
                else
                    ch[i][j] = 0;
            }  
        }
        if(N * M - wall <= T || (abs(index_i - start_i) + abs(index_j - start_j) + T) % 2) { //粗略步数剪枝, 奇偶剪枝
            printf("NO\n");
            continue;
        }
        biaoji = 0;
        dfs(start_i, start_j, T);
        if(biaoji)
            printf("YES\n");
        else
            printf("NO\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值