• hdu 1010 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): 82058    Accepted Submission(s): 22370


    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


    初次看到这道题,第一反应搜索模板,直接开写,几分钟就搞定,测试样例也过了,奈何数次TLE(超时),继而修改无果,很愧疚的百度了题解,好吧,,果真是剪枝,看来以后要加强学习了,很典型的奇偶剪枝。

    直接上AC代码

    <span style="font-size:18px;">#include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    using namespace std ;
    
    char mp[100][100] ;
    int m,n,t ;
    bool hasFind ;
    int ex,ey,sx,sy ;
    int dir[4][2] = {
        1,0,
        -1,0,
        0,-1,
        0,1
    } ;
    
    
    int inMap(int x,int y)  //判断是否出界
    {
        return (x>=0 && x<n && y>=0 && y<m) ;
    }
    
    void dfs(int x,int y,int time)  //深度优先搜索
    {
        if(x==ex && y==ey && time==t)
        {
            hasFind = true ;
        }
        if(hasFind)
            return ;
       // int v=t-time-abs(ex-x)-abs(ey-y);//据奇偶性剪枝
        if( (abs(ex-x)+abs(ey-y))%2 != (t-time)%2 )   //奇偶剪枝
            return ;
        for(int i = 0 ;i<4 ;i++)
        {
    
            int tx = x+dir[i][0] ;
            int ty = y+dir[i][1] ;
            if(inMap(tx,ty) && mp[tx][ty]!='X')
            {
                mp[tx][ty] = 'X' ;
                dfs(tx,ty,time+1) ;
                mp[tx][ty] = '.' ;
            }
        }
    }
    
    int main()
    {
        while(scanf("%d%d%d",&n,&m,&t)!=EOF && (n+m+t))
        {
            int ti = 0 ;
            for(int i = 0 ;i<n ;i++)
                for(int j = 0 ;j<m ;j++)
            {
                cin>>mp[i][j] ;
                if(mp[i][j] == 'D')
                {
                    ti++ ;
                    ex = i ;
                    ey = j ;
                }
                else if(mp[i][j] == '.')
                {
                    ti++ ;
                }
                else if(mp[i][j] == 'S')
                {
                    sx = i ;
                    sy = j ;
                }
            }
            mp[sx][sy] = 'X' ;   //此步很关键
            hasFind = false ;
            if(ti < t)            //如果图上所有可走的点数加起来都达不到t,那么绝不可能在t秒刚刚好到达门口
            {
                printf("NO
    ") ;
                continue ;
            }
            dfs(sx,sy,0) ;
            if(hasFind)
            {
                printf("YES
    ") ;
            }
            else printf("NO
    ") ;
        }
    }
    </span>


  • 相关阅读:
    放弃模拟器,安卓手机WiFi投屏到Ubuntu
    在Ubuntu中使用多显示器远程window主机
    完全卸载visual studio及其组件
    使用defineProperty实现自定义setter, 简化前端Angular的重构工作
    数字图像基础
    Python的文本和字节序列
    机器学习开发流程基础
    深度学习之新闻多分类问题
    深度学习之电影二分类的情感问题
    Chrome89针对sessionStorage的更新导致数据共享问题
  • 原文地址:https://www.cnblogs.com/emoji/p/4436783.html
Copyright © 2020-2023  润新知