HDU-1010-Tempter of the Bone(搜索奇偶剪枝)

<span style="white-space:pre">						</span>Tempter of the Bone

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


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


在开搜索专题的时候,看到了这道题目。脑子里第一反应,这是个水题啊!然后啪啪啪一通bfs就干了上去,果断返回WA,改了几发,却始终是WA。然后又仔仔细细的重新读了一遍题目,发现读错题了!这个错误...以后一定要改正。
关于在做这道题目时候的几点想法:
1、之所以不能用bfs是因为bfs每当到达一个新的坐标时就要将标记数组标记为我来过这个点了,但其严重后果是最后答案可能不是这一条路,但你曾经标记过,途经的点被标记过再也过不去了,所以会返回NO,但结果是YES。如果想把标记数组去掉,直接暴力去自己找会直接MLE,相信我,我不会告诉你我都试过了...
2、做题的时候需要注意的一点是,当当前步数已经超过t时就要回溯了,不应再继续,否则会TLE。
3、最重要的一点来了
(t - abs(sx - ex) - abs(sy - ey) - num ) < 0 || (t - abs(sx - ex) - abs(sy - ey) - num ) % 2)
详情原因见奇偶剪枝_百度百科,第一次见百度百科讲的好详细。
4、剩下的、、剩下的就没啥了、、see code

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1
const int M = 220;
const double esp = 1e-6;
const double PI = 3.14159265359;
const int INF = 0x3f3f3f3f;
using namespace std;


int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int n,m,s,vis[10][10];
char Map[10][10];
bool flag ;
bool judge(int x,int y){
    if(x >=0 && x < n && y >= 0 && y < m && Map[x][y]!='X')
        return true;
    return false;
}


void cal(int sx,int sy,int ex,int ey,int t,int num){
    if(flag || num > t)
        return ;
    if(sx == ex && sy == ey && num == t){
        flag = true;
        return ;
    }
    if((t - abs(sx - ex) - abs(sy - ey) - num ) < 0 || (t - abs(sx - ex) - abs(sy - ey) - num ) % 2)
        return ;
    for(int i=0;i<4;i++){
        int nowx = sx + dx[i];
        int nowy = sy + dy[i];
        if(judge(nowx,nowy) && !vis[nowx][nowy]){
            //printf("x = %d y = %d num = %d\n",nowx,nowy,num+1);
            vis[nowx][nowy] = true;
            cal(nowx,nowy,ex,ey,t,num+1);
            vis[nowx][nowy] = false;
        }
    }
    return;
}


int main(){
    int sx,sy,ex,ey;
    while(~scanf("%d %d %d",&n,&m,&s) && (n||m||s)){
        flag = false;
        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;
                if(Map[i][j] == 'D')
                    ex = i,ey = j;
            }
        }
        memset(vis,false,sizeof(vis));
        vis[sx][sy] = true;
        cal(sx,sy,ex,ey,s,0);
        printf(flag?"YES\n":"NO\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值