zoukankan      html  css  js  c++  java
  • hdu1010dfs

    Tempter of the Bone

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


    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
     

    这题好像只能用dfs,因为并不是去求最短路径,到达‘D’需要刚好到达limit时间,所有要讨论多条路径,bfs可取

    还有这题剪枝剪的很厉害,数据比较强,

    考虑两方面,一方面是奇偶性,一方面是bfs的出口,如果已经找到了路径,如果还未找到,时间已经达到limit,都可以剪枝

    其实已经做过了,但是还是卡了很久,毕竟菜,重要不要失去信心就好

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <stack>
    #include <queue>
    
    const int inf = (1<<31)-1;
    const int MAXN = 1e1;
    using namespace std;
    
    char G[MAXN][MAXN];
    int n,m,tt,flag;
    int mov[4][2]={-1,0,1,0,0,1,0,-1};
    
    int check(int x,int y){
        if(x<1||y<1||x>n||y>m)return 0;
        if(G[x][y]=='X')return 0;
        else return 1;
    }
    
    void dfs(int x,int y,int t){
        if(flag)return;//继续剪枝
        if(t>tt)return;
        if(G[x][y]=='D'){
            if(tt==t)
                flag = 1;
           // cout<<"ok"<<endl;
        }
        int nx,ny;
        if(G[x][y]=='.'){
            for(int i=0;i<4;i++){
                nx = x+mov[i][0];
                ny = y+mov[i][1];
                if(check(nx,ny)){
                    G[x][y] = 'X';
                    dfs(nx,ny,t+1);
                    G[x][y] = '.';
                }
            }
        }
    }
    
    int main()
    {
        while(scanf("%d%d%d",&n,&m,&tt),n){
            flag = 0;
            int sx,sy,dx,dy;
            for(int i=1;i<=n;i++){
                scanf("%s",G[i]);
                for(int j=m;j>=1;j--){
                    G[i][j] = G[i][j-1];
                    if(G[i][j]=='S'){
                        sx = i;
                        sy = j;
                    }else if(G[i][j]=='D'){
                        dx = i;
                        dy = j;
                    }
                }
            }
            int tp  = fabs((sx-dx)*1.)+fabs((sy-dy)*1.);
            if(tp%2==tt%2&&tp<=tt){ //剪枝
                G[sx][sy]='.';
                dfs(sx,sy,0);
            }
            G[sx][sy]='.';
            dfs(sx,sy,0);
            if(flag)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
    }
    View Code
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    保留字段数组,一定要用char
    VirtualBox安装CentOS 7及其相关配置
    istringstream是支持>>一个bool的,但为什么不用?
    用vector实现一个变长数组
    C语言为什么被const声明的变量不是一个常量表达式
    不咬文嚼字的理由
    int变量赋值给char变量的本质
    #include <> 和 #include "" 的区别
    C++中匿名对象应当是一个左值
    js实战之-间断文字滑动
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5430431.html
Copyright © 2011-2022 走看看