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;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    在网上如何找靠谱的兼职?
    Amphorae 与 Octavia Worker 的安全通信实现
    idea遇到XXXXXXXX.java文件上出现小叉号
    2019创业做哪方面,有什么好的建议吗?
    投资理财财经 现在每个月可以抽出2000元的闲钱,投资什么项目比较好?
    开水果蔬菜超市利润大吗?
    理财能不能发家致富?
    不想打工了,手里有10万,想回农村老家创业,却不知道做什么,请各位指点迷津,谢谢?
    为什么很多人说现在做什么生意都不好做?
    ylbtech-SubwayNav(地铁线路导航)-数据库设计
  • 原文地址:https://www.cnblogs.com/l609929321/p/7018520.html
Copyright © 2011-2022 走看看