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;
    }
    


  • 相关阅读:
    【原】 OPenCV学习笔记1:imread()
    windows8下:OpenCV2.2.0 +VS2005/2008/2010
    数组指针和指针数组的区别
    虚函数和纯虚函数 覆盖和隐藏
    C/C++文件之eof()
    【转】 CvArr、Mat、CvMat、IplImage、BYTE转换(总结而来)
    HTML 5是如何流行起来的
    Java程序员要注意的10个问题————————好东西就是要拿来分享
    祈福雅安
    对软件工程的思考
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312747.html
Copyright © 2011-2022 走看看