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

    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
    很多人不明白偶数剪枝怎么来的,想一下,一个点出发,再回到它自己 一定走了奇数步,故有第二个剪枝
    #include<map>
    #include<set>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
    
    using namespace std;
    
    const double pi=acos(-1.0);
    const double eps=1e-8;
    typedef pair<int,int>pii;
    
    int dx[4]={-1,1,0,0};
    int dy[4]={0,0,-1,1};
    bool vis[8][8],cut;
    int x1,x2,y11,y2,n,m,T;
    
    char str[8][8];
    void dfs(int x,int y,int step)
    {
        if (cut) return;
        if (step>T) return;
        if (x==x2 && y==y2 && step==T)
        {
            cut=true;
            return;
        }
        if (step==T) return ;
        if (T-step<abs(x2-x)+abs(y2-y) || (T-step-abs(x2-x)+abs(y2-y))%2) return;
        for (int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=y+dy[i];
            if (xx>=0 && xx<n && yy>=0 && yy<m && !vis[xx][yy] && str[xx][yy]!='X')
            {
                vis[xx][yy]=true;
                dfs(xx,yy,step+1);
                vis[xx][yy]=false;
            }
        }
    }
    
    
    int main()
    {
        //freopen("in.txt","r",stdin);
    
        while (scanf("%d%d%d",&n,&m,&T)!=EOF && !(n==0 && m==0 && T==0))
        {
            for (int i=0;i<n;i++) scanf("%s",str[i]);
            for (int i=0;i<n;i++)
            for (int j=0;j<m;j++)
            {
                if (str[i][j]=='S') {x1=i;y11=j;}
                if (str[i][j]=='D') {x2=i;y2=j;}
            }
            cut=false;
            memset(vis,0,sizeof(vis));
            vis[x1][y11]=true;
            dfs(x1,y11,0);
            if (cut) printf("YES
    ");
            else printf("NO
    ");
        }
    
        //fclose(stdin);
        return 0;
    }
     
    至少做到我努力了
  • 相关阅读:
    WEB开发者必备的7个JavaScript函数
    json分别算出元素的个数和最多的元素
    jquery 对 Json 的各种遍历
    判断图片是否加载完成
    obj转换成数组
    用JavaScript获取页面上被选中的文字的技巧
    PAT 1088 三人行(20 分)(暴力破解+流程分析)
    PAT 1087 有多少不同的值(20)(STL-set代码)
    PAT 1086 就不告诉你(15 )(代码)
    PAT 1087 有多少不同的值(20)(STL—set)
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3690793.html
Copyright © 2011-2022 走看看