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

    Tempter of the Bone

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


    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

    题意:一个迷宫,要刚好在t秒的时候走到门口才可以,每一次走过一个格子就不能再走这个格子了,问可不可以走出来
    思路:dfs搜索,如果时间t比所有格子的数量都多那肯定走不出来了,画一画就知道如果绕一绕的话肯定是增加偶数步的,那就可以进行剪枝了,如果不可以的话为了继续找寻路,记得把走过的那个点标记成未走过哦
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #include<set>
    #include<map>
    #include<vector>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define eps 1e-10
    typedef long long ll;
    const int maxn = 1e2+5;
    const int mod = 1e9 + 7;
    int gcd(int a, int b) {
        if (b == 0) return a;  return gcd(b, a % b);
    }
    int N,M,T,startx,starty,endx,endy;
    char maze[maxn][maxn];
    int dx[4]={1,0,-1,0};
    int dy[4]={0,1,0,-1};
    int step;
    int ok;
    void dfs(int x,int y,int step)
    {
        int temp;
        if(x==endx && y==endy && step==T)
        {
            ok=1;
            return ;
        }
        if(x<=0 || x>N || y<=0 || y>M)
            return ;
        temp=abs(T-step)-(abs(x-endx)+abs(y-endy));
        if(temp<0 || temp&1)
            return ;
        for(int i=0;i<4;i++)
        {
            int nx=x+dx[i],ny=y+dy[i];
            if(maze[nx][ny]!='X')
            {
              maze[nx][ny]='X';
              dfs(nx,ny,step+1);
              if(ok)
                  return;
              maze[nx][ny]='.';
            }
        }
        return ;
    }
    int main()
    {
        while(scanf("%d%d%d",&N,&M,&T) && (N&&M&&T))
        {
            int num=0;
            for(int i=1;i<=N;i++)
                for(int j=1;j<=M;j++)
                {
                    cin>>maze[i][j];
                    if(maze[i][j]=='S')
                    {
                        startx=i;
                        starty=j;
                    }
                    else if(maze[i][j]=='D')
                    {
                        endx=i;
                        endy=j;
                    }
                    else if(maze[i][j]=='X')
                        num++;
                }
            if(N*M-num<=T)
            {
                cout<<"NO"<<endl;
                continue;
            }
            ok=0;
            maze[startx][starty]='X';
                    dfs(startx,starty,0);
            if(ok)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
            
        }
    }
  • 相关阅读:
    Linux中rar解压软件
    Linux中rpm和yum安装软件
    查看nova日志
    po编译为mo;django翻译多义性问题解决
    某服務器開端口
    linux環境查找某文件夾下含有某字符串的所有文件
    gerrit +git使用
    ubuntu 暂时使用root权限
    mysql-求中位数方法
    phonecat-angular.js route.js加载启动失败
  • 原文地址:https://www.cnblogs.com/smallhester/p/9500549.html
Copyright © 2011-2022 走看看