HDU 1010 Tempter of the Bone 深搜+奇偶剪枝

来源:互联网 发布:手机麻将辅助软件 编辑:程序博客网 时间:2024/05/21 09:54



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


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
NOYES
 

Author
ZHANG, Zheng
题意:就是在t步之内从S走到D;
思路:从开始处进行深搜,剪枝很多最主要的有奇偶剪枝
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n,m,t;
char gra[10][10];
int sx,sy,ex,ey;
int sum;
int tag;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
int ok(int x,int y){
    if(x<0||y<0||x>=n||y>=m){
        return 0;
    }
    if(gra[x][y]=='X'){
        return 0;
    }
    return 1;
}
void dfs(int x,int y){
    if(x == ex&&y == ey&&sum == t){
        tag = 1;
        return ;
    }
    if(tag==1){ return ;}//找到即退出,不然会超时
     if(sum >= t){
        return ;
    }
    int tt = t-sum-abs(x-ex)-abs(y-ey);//计算当前位置距离目标的距离(即偏移距离)
    if(tt < 0 || tt%2!=0){  return ; }
    for(int i = 0;i < 4;i++){
        if(ok(x+xx[i],y+yy[i])){
            gra[x][y]='X';
            sum++;
            dfs(x+xx[i],y+yy[i]);
            gra[x][y]='.';
            sum--;
        }
    }
}
int main(){
    while(~scanf("%d%d%d",&n,&m,&t)&&n&&m&&t){
        for(int i = 0;i < n;i++){
            scanf("%s",gra[i]);
            for(int j = 0;j < m;j++){
                if(gra[i][j] == 'S'){
                    sx = i;
                    sy = j;
                }
                if(gra[i][j] == 'D'){
                    ex = i;
                    ey = j;
                }
            }
        }
        sum = 0;
        tag = 0;
        dfs(sx,sy);
        if(tag){
            printf("YES\n");continue;
        }
        printf("NO\n");
    }
    return 0;
}
注意:fsbs()在G++上可以ac,但在c++上不行,改成abs()就可以了。理由:abs是求整数的绝对值,fabs 是求实数的绝对值,主要的是类型不对,还有几个剪枝位置不能调换,
我就疯了这个题先是TLE后面就是WA,就是因为我把tt写成t,这是粗心。。。以后自习认真的做,之前自己还写过这个的博客又忘了这些东西。。。。。。。。
AC代码:
 
0 0
原创粉丝点击