zoukankan      html  css  js  c++  java
  • hdu 1010

    Tempter of the Bone

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

    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
     
    分析:深搜+剪枝。
    可以用direction[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}分别控制右、下、左、上。

    主要的剪枝条件有:

    1、剩余可走区域小于时间

    2、奇偶性剪枝

    3、越界

    4、超时

     

    下面主要说说奇偶性剪枝 若有一迷宫,将迷宫的每一个位置有0或1表示(x+y为偶数时 为0 否则为1):

    0    1    0    1    0

    1    0    1    0    1

    0    1    0    1    0

    1    0    1    0    1

    从图中我们可以很清晰的看出:任意一个位置周围相邻的必然是与本身值相反的值,也就是说,要想走到与本身值相同的点必然要走偶数步; 同理,要想走到与本身相异的值的点必然要走奇数步; 所以,当两个位置的奇偶性相同时(同为0或同为1)若时间为奇数  则必然无法到达;当两个位置的奇偶性不同时(一个为1,另一个为0)若时间为偶数也必不能到达。

    代码实现:if((endi+starti+startj+endj+t)%2==1)
                {
                    printf("NO ");
                    continue;
                } 

    超时:若N*M-(X的数量)<T,则当到门的点时,门并没有打开。

    #include<stdio.h>
    char map[7][7];
    int starti,startj,endi,endj,t,n,m,flag;
    int direction[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
    void DFS(int x,int y,int time)
    {
        int i;
        if(flag==1)
            return;
        if(map[x][y]=='X')
            return;
        if(x==endi&&y==endj&&time==t)
        {
            flag=1;
            return;
        }
        if(x<1||x>n||y<1||y>m) //越界
            return;
        if(time>t)
            return;
        for(i=0; i<4; i++)
        {
            map[x][y]='X';           //对将要被访问的点进行标记
            DFS(x+direction[i][0],y+direction[i][1],time+1);
            map[x][y]='.';        //回溯
            if(flag==1)
                return;
        }
    }
    int main()
    {
        int i,j,blockcount;
        while(~scanf("%d%d%d",&n,&m,&t)&&(n||m||t))
        {
            getchar();
            blockcount=0;
            flag=0;
            for(i=1; i<=n; i++)
            {
                for(j=1; j<=m; j++)
                {
                    scanf("%c",&map[i][j]);
                    if(map[i][j]=='S')
                    {
                        starti=i;
                        startj=j;
                        continue;
                    }
                    if(map[i][j]=='X')
                    {
                        blockcount++;
                        continue;
                    }
                    if(map[i][j]=='D')
                    {
                        endi=i;
                        endj=j;
                    }
                }
                getchar();
            }
            if(t>(n*m-blockcount))
            {
                printf("NO ");
                continue;
            }
            if((endi+starti+startj+endj+t)%2==1)
            {
                printf("NO ");
                continue;
            }
            DFS(starti,startj,0);
            if(flag==1)
                printf("YES ");
            else
                printf("NO ");
        }
        return 0;
    }

  • 相关阅读:
    清北学堂 清北-Day1-R1-Count
    清北学堂 清北-Day1-R2-监听monitor
    LuoGu P2420 让我们异或吧
    Milk(sort+结构体)
    开门人和关门人(结构体+sort)
    python-神奇的下划线
    python-pathlib
    python-文本字符串
    python-闭包
    进制-Iterative-进制转换
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3227933.html
Copyright © 2011-2022 走看看