zoukankan      html  css  js  c++  java
  • HDU 1010 dfs+奇偶剪枝

    dfs+剪枝

    Tempter of the Bone

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


    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到达出口

    读题要仔细,这个并不是求最短路,而是在求固定步数

    思路:开始我没仔细读题直接用了bfs,果断wa了。后来用普通dfs结果TLE,最后看题解才知道用dfs+奇偶剪枝

    奇偶剪枝介绍如下:



    在dfs中具体应用就是,T-cnt(也就是剩余步数)t要和abs(x-target.x)+abs(y-target.y)的奇偶性一致,否则不能到达


    #include<string>
    #include<string.h>
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<cmath>
    #include<stack>
    #include<algorithm>
    #include<queue>
    using namespace std;
    int dir[][2]= {{0,1},{0,-1},{1,0},{-1,0}};
    typedef struct Point
    {
        int x;
        int y;
    } point;
    int m_map[50][50];
    int vis[50][50];
    queue<Point>que;
    int n,m,T;
    int m_flag;
    point start,target;
    /*void bfs(point start,point target)
    {
        int i,j,k,xx,yy;
        bool flag=false;
        while(!que.empty()) que.pop();
        que.push(start);
        vis[start.x][start.y]=1;
        point temp1,temp2;
        while(!que.empty())
        {
            temp1=que.front();
            que.pop();
            for(i=0; i<4; i++)
            {
                xx=temp1.x+dir[i][0];
                yy=temp1.y+dir[i][1];
                printf("(%d %d)
    ",xx,yy);
                if(xx>=0&&xx<n&&yy>=0&&yy<m&&m_map[xx][yy]!=-1&&vis[xx][yy]==0)
                {
                    temp2.x=xx;
                    temp2.y=yy;
                    que.push(temp2);
                    vis[xx][yy]=vis[temp1.x][temp1.y]+1;
                }
    
            }
    
            if(vis[target.x][target.y]>0)
            {
                flag=true;
                break;
            }
        }
        if((vis[target.x][target.y]-1)<=T&&flag)
        {
            printf("YES
    ");
        }
        else printf("NO
    ");
    }*/开始傻乎乎用bfs
    
    void dfs(int x,int y,int cnt)
    {
        int i,j,k,xx,yy;
        if(x==target.x&&y==target.y)
        {
            if(cnt==T)
                m_flag=1;
            return ;
        }
        if(T-cnt<abs(x-target.x)+abs(y-target.y)||(T-cnt+abs(x-target.x)+abs(y-target.y))%2!=0)return ;//奇偶剪枝
        for(i=0; i<4; i++)
        {
            xx=x+dir[i][0];
            yy=y+dir[i][1];
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&m_map[xx][yy]!=-1&&vis[xx][yy]==0)
            {
                vis[xx][yy]=1;
                dfs(xx,yy,cnt+1);
                vis[xx][yy]=0;
                if(m_flag)return ;
            }
        }
    }
    int main()
    {
        int i,j,k;
        char ch;
        while(scanf("%d%d%d",&n,&m,&T)!=EOF)
        {
            getchar();
            m_flag=0;
            if(n==0&&m==0&&T==0)break;
            memset(m_map,0,sizeof(m_map));
            memset(vis,0,sizeof(vis));
            for(i=0; i<n; i++)
            {
                for(j=0; j<m; j++)
                {
                    scanf("%c",&ch);
                    if(ch=='X')m_map[i][j]=-1;
                    else if(ch=='S')
                    {
                        start.x=i;
                        start.y=j;
                    }
                    else if(ch=='D')
                    {
                        target.x=i;
                        target.y=j;
                    }
                }
                getchar();
            }
            //        for(i=0; i<n; i++)
            //        {
            //            for(j=0; j<m; j++)
            //                printf("%d ",m_map[i][j]);
            //            printf("
    ");
            //
            //        }
            if((start.x+target.x+start.y+target.y+T)%2==1||(abs(start.x-target.x)+abs(start.y-target.y))>T)//这个其实也是奇偶剪枝,有木有无所谓
            {
                printf("NO
    ");
                continue;
            }
            vis[start.x][start.y]=1;//这步很重要,dfs前一定要把起始点标志为走过
            dfs(start.x,start.y,0);
            if(m_flag)
            {
                printf("YES
    ");
            }
            else printf("NO
    ");
    
        }
    
        return 0;
    }
    /*
    4 4 5
    S.X.
    ..X.
    ..XD
    ....
    3 4 5
    S.X.
    ..X.
    ...D
    4 4 5
    S.X.
    ..XD
    ..XX
    ....
    0 0 0
    */
    


    补充:这段代码执行了468ms,然而再在main里再加上一个剪枝就变成78ms

            if(m*n<T+1+k)
            {
               printf("NO ");
                continue;
            }

    我是看其他大佬的代码发现的,我是这么理解的迷宫共有m*n个格,由空格,墙,起始点,终点构成,k为墙数

    那么就满足,m*n>=t+1+k画个图就明白了‘1’代表‘起始点占一个格,’k‘代表墙占k个格





  • 相关阅读:
    小编见过的验证方式汇总
    Burp Suite Professional 针对APP抓包篡改数据提交【安全】
    关于Chrome 67 以后版本无法离线安装扩展的解决方法
    Postman 中上传图片的接口怎么做参数化呢?
    字符串-不同的编码格式下所占用的字节数【转】
    Java RandomAccessFile用法 【转】
    URI和URL的区别 【转】
    Java多线程-线程的同步与锁【转】
    浅谈Java多线程的同步问题 【转】
    Java中浮点数的精度问题 【转】
  • 原文地址:https://www.cnblogs.com/hjch0708/p/7554812.html
Copyright © 2011-2022 走看看