zoukankan      html  css  js  c++  java
  • hdu 1010 深搜

    http://acm.hdu.edu.cn/showproblem.php?pid=1010

    Tempter of the Bone

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

    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
     
    Recommend
    JGShining
     
    这个题目一直超时了很多次,还有WA,关键是剪枝和输入scanf时要注意
    关于剪枝:
    1:记录地图中能够走的点数sum,如果sum>=t  则可以继续搜索,反之输出NO(优化这一步从500Ms 直接到100多MS)。
    2:在开始和Dfs 搜索函数中加入 奇偶剪枝,意思是从当前点走到最终点的步数必须和剩余的步数的奇偶性相同,abs(ed1-st1)+abs(ed-st)-t)%2==0
    奇偶性不同则不可能走到终点。
    3:Dfs函数中走到终点标记flag为1,并判断当flag为一时返回。
    //用 scanf 输入的请况 ,注意getchar(); 或则去除scanf和getchar(); 用cin也能AC
    #include<iostream>
    using namespace std;
    // #include <stdio.h>
    int n,m,t;
    char Maze[8][8];
    int di[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
    int sum;
    int st,st1,ed,ed1;
    int flag;
    
    int abs(int x)
    {
        return x>0?x:-x;
    }
    
    void Dfs(int x,int y,int e)
    {
        /*if(x>n||x<1||y>m||y<1)
            return ;*/
        if(x==ed&&y==ed1&&t==e)      //搜索终点,并标记flag-1
        {
            flag=1;
            return ;
        }
        if(flag==1)
            return ;
        if(abs(ed1-y)+abs(ed-x)>(t-e)||(abs(ed1-y)+abs(ed-x)-(t-e))%2!=0)  //剪枝
            return ;
    
        for(int i=0;i<4;i++)
        {
            if(x+di[i][0]>n||x+di[i][0]<1||y+di[i][1]>m||y+di[i][1]<1)
                continue;
            if(Maze[x+di[i][0]][y+di[i][1]]!='X')
            {
                Maze[x+di[i][0]][y+di[i][1]]='X';
                Dfs(x+di[i][0],y+di[i][1],e+1);
                Maze[x+di[i][0]][y+di[i][1]]='.';
            }
        }
        return ;
    }
    
    
    int main()
    {
        int i,j;
        while(scanf("%d%d%d",&n,&m,&t)==3&&(n+m+t))
        {
            sum=0;
            flag=0;
            getchar();
            for(i=1;i<=n;i++)    
            {
                for(j=1;j<=m;j++)
                {
                    scanf("%c",&Maze[i][j]);
                //    cin>>Maze[i][j];
                    if(Maze[i][j]=='S')
                    {
                        st=i;
                        st1=j;
                    }
                    else if(Maze[i][j]=='D')
                    {
                        ed=i;
                        ed1=j;
                        sum++;
                    }
                    else if(Maze[i][j]=='.')
                    sum++;                                         //标记能够走的步数
                }
                    getchar();
            }
            Maze[st][st1]='X';
            if(sum>=t&&t>=abs(ed1-st1)+abs(ed-st)&&(abs(ed1-st1)+abs(ed-st)-t)%2==0)   
            // print();
            Dfs(st,st1,0);
            if(flag)
                printf("YES\n");
            else
                printf("NO\n");
        }
       return 0;
    }
     
  • 相关阅读:
    【搜索引擎】Solr最新安装以及通过关系型数据库(MySQL,Oracle,PostgreSQL)导入数据
    【搜索引擎】SOLR VS Elasticsearch(2019技术选型参考)
    【Java源码】集合类-优先队列PriorityQueue
    【Java源码】树-概述
    Go语言调度器之创建main goroutine(13)
    Go语言goroutine调度器初始化(12)
    Go语言goroutine调度器概述(11)
    线程本地存储及实现原理
    操作系统线程及线程调度
    系统调用
  • 原文地址:https://www.cnblogs.com/hzg656343072/p/2630582.html
Copyright © 2011-2022 走看看