zoukankan      html  css  js  c++  java
  • Tempter of the Bone HDU 1010

    Tempter of the Bone

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


    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
     
    Author
    ZHANG, Zheng
     
    Source
     
    Recommend
    JGShining
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    int n,m,t,ax,ay,bx,by;
    char mapn[10][10];
    int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
    int vis[10][10],flag;
    void dfs(int x,int y,int cnt)
    {
        if(x==bx&&y==by)
        {
            if(t == cnt)
                flag = 1;
            return;
        }
        if(cnt >= t)
            return;
        for(int i=0;i<4;i++)
        {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(mapn[xx][yy]=='X'||xx<1||xx>n||yy<1||yy>m||vis[xx][yy])
                continue;
            vis[xx][yy] = 1;
            dfs(xx,yy,cnt+1);
            vis[xx][yy] = 0;
            if(flag)
                return;//注意如果已经找到所需结果,不要再继续搜索,浪费时间
        }
    }
    int main()
    {
        while(cin >> n >> m >> t)
        {
            if(!n&&!m&&!t)
                break;
            for(int i=1;i<=n;i++)
            {
                getchar();
                for(int j=1;j<=m;j++)
                {
                    cin >> mapn[i][j];
                    if(mapn[i][j] == 'S')
                    {
                        ax = i;
                        ay = j;
                    }
                    if(mapn[i][j] == 'D')
                    {
                        bx = i;
                        by = j;
                    }
                }
            }
            getchar();
            memset(vis,0,sizeof(vis));
            if(abs(ax-bx)+abs(ay-by)>t||(ax+bx+ay+by+t)%2==1)//注意这道题目要进行剪枝,以后如果再遇到搜索题目,应该先排除不可能的情况,再搜索
            {
                cout << "NO" << endl;
                continue;
            }
            vis[ax][ay] = 1;
            flag = 0;
            dfs(ax,ay,0);
            if(flag)
                cout << "YES" << endl;
            else cout << "NO" << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    selenium加载配置参数,让chrome浏览器不出现‘Chrome正在受到自动软件的控制’的提示语,以及后台静默模式运行,不占用桌面的方法
    CentOS7使用firewalld打开关闭防火墙与端口
    LVM基本介绍与常用命令
    CentOS 7 网络配置详解
    rm删除破折号开头的文件或目录
    linux时间的查看与修改
    linux 下shell中if的“e,d,f”是什么意思
    selenium之 定位以及切换frame(iframe)
    Unix/Linux 命令速查表
    History(历史)命令用法 15 例
  • 原文地址:https://www.cnblogs.com/l609929321/p/7018520.html
Copyright © 2011-2022 走看看