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

    Tempter of the Bone

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 540 Accepted Submission(s): 187
     
    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
    ZJCPC2004
     

    思路:典型dfs,并且必须进行奇偶剪枝否则会TLE。奇偶剪枝的含义是,x1-x2+y1-y2的奇偶性和从(x1,y1)到(x2,y2)的步数奇偶性一致。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<queue>
    using namespace std;
    int m,n,a,b,T;
    int dx[]={0,0,1,-1};
    int dy[]={1,-1,0,0};
    int vis[10][10];
    char map[10][10];
    int c,d;
    int dfs(int x,int y,int now)
    {
        int i,xx,yy;
        if(now==T)
        {
            if(map[x][y]=='D')
                return 1;
            else
                return 0;
        }
        if((T-now+c-x+d-y)%2)
            return 0;
        for(i=0;i<4;++i)
        {
            xx=x+dx[i];
            yy=y+dy[i];
            if(xx<0|| xx>=m || yy<0 || yy>=n || vis[xx][yy] || map[xx][yy]=='X')
                continue;
            vis[xx][yy]=1;
            if(dfs(xx,yy,now+1))
                return 1;
            vis[xx][yy]=0;
        }
        return 0;
    }
    int main()
    {
        int i,j;
        while(scanf("%d%d%d",&m,&n,&T)!=EOF && m)
        {
            scanf("%*c");
            for(i=0;i<m;++i)
            {
                scanf("%s",map[i]);
                for(j=0;j<n;++j)
                {    if(map[i][j]=='S')
                    {
                        a=i;
                        b=j;
                    }
                else if(map[i][j]=='D')
                {
                    c=i;
                    d=j;
                }
                }
            }
            memset(vis,0,sizeof(vis));
            vis[a][b]=1;
            printf("%s\n",dfs(a,b,0)?"YES":"NO");
        }
        return 0;
    }
  • 相关阅读:
    Oracle 备份与恢复 15 个典型问题
    Oracle Rman 增量备份与差异备份
    Oracle top 查询TOP SQL
    Oracle 将另外一张表的列更新到本表的列
    Mysql Innodb 表碎片整理
    python Django 之 Model ORM inspectdb(数据库表反向生成)
    MySQL 5.6比较重要的参数,以及5.5到5.6默认值有过变化的参数
    Python Django 前后端数据交互 之 HttpRequest、HttpResponse、render、redirect
    HTML(一)基础
    Python Django 前后端数据交互 之 HTTP协议下GET与POST的区别
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2654708.html
Copyright © 2011-2022 走看看