zoukankan      html  css  js  c++  java
  • HDU

    Tempter of the Bone

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


    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题,我交了13遍才AC的么?

    刚开始用的BFS,因为是最短路,但是题目要求比较多,不好做标记,改为DFS。

    但是DFS慢啊,需要各种剪枝特判。


    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    struct dot{
        int r, c;
    } bg, ed;
    bool ok;
    int n, m, t;
    char tab[10][10];
    bool vis[10][10];
    const int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
    
    bool hefa(const dot & x, const int cnt)
    {
        if(ok) return false;
        if(abs(ed.r - x.r) + abs(ed.c - x.c) > t - cnt) return false;
        if((x.r + x.c + ed.r + ed.c + t - cnt) & 1) return false;
        if(x.r < 0 || x.c < 0 || x.r >= n || x.c >= m) return false;
        if(tab[x.r][x.c]=='X' || vis[x.r][x.c]) return false;
        return true;
    }
    
    void DFS(const dot cur, const int cnt)
    {
        if(cnt > t) return;
        if(cur.r==ed.r && cur.c==ed.c && cnt==t){
            ok = true;
            return;
        }
        for(int i = 0; i < 4; i++){
            dot next; next.r = cur.r + dir[i][0]; next.c = cur.c + dir[i][1];
            if(hefa(next, cnt + 1)){
                vis[next.r][next.c] = true;
                DFS(next, cnt + 1);
                vis[next.r][next.c] = false;
            }
        }
        return;
    }
    
    int main()
    {
        while(~scanf("%d %d %d", &n, &m, &t) && !(n==0 && m==0 && t==0))
        {
            getchar();
            ok = false;
            memset(tab, 0, sizeof(tab));
            memset(vis, false, sizeof(vis));
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    scanf("%c", &tab[i][j]);
                    if(tab[i][j]=='S') bg.r = i, bg.c = j;
                    if(tab[i][j]=='D') ed.r = i, ed.c = j;
                }
                getchar();
            }
            if(!hefa(bg, 0)){
                puts("NO");
                continue;
            }
            vis[bg.r][bg.c] = true;
            DFS(bg, 0);
            if(ok) puts("YES");
            else puts("NO");
        }
        return 0;
    }
    


  • 相关阅读:
    vs中nodejs代码 resharper 提示 ECMAScript2015 Feature. your Current language level is ECMAScript5的解决办法
    port: ${SERVER_PORT:9190} #首先取环境变量,如果环境变量中没有,就取 9190 这个固定值
    SpringbBoot之JPA批量更新
    Linq To Object多字段组合唯一校验
    LINQ获取两个List的交集
    Linq实现分组后取最大(小)值
    Maven:浅析依赖(dependency)关系中 scope 的含义
    Source roots (or source folders) Test source roots (or test source folders; shown as rootTest)Resource rootsTest resource roots
    Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:
    sping boot/cloud配置文件 on 读取为true
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312747.html
Copyright © 2011-2022 走看看