zoukankan      html  css  js  c++  java
  • HDOJ 1010 (DFS,剪枝)

    Tempter of the Bone

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


    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
     
     
    Conclude:
    1.DFS奇偶性剪枝,否则超时;
    2.多字符串输入只要有空格或者换行,就不能用scanf %c;
    3.内外两个回溯;
     
     
    #include<iostream>
    #include<cstring>
    using namespace std;
    char a[7][7];
    int vis[7][7];          //标记能走的位置(除去出界的位置,走过的位置和不能走的位置) 
    int n,m,t;              //如此就不在递归中判断出界 
    int x1,y1;
    int x,y;
    bool flag;
    int b[4]={1,-1,0,0};
    int c[4]={0,0,1,-1};
    
    int abs(int n)         //发现如果包含cmath头文件中 ,x0,y0,x1,y1等会产生歧义 
    {
        if(n<0)
           n=-1*n;
        return n;
    }
    
    void dp(int k)
    {    
    //    if(x1<0||x1>n||y1<0||y1>m)
    //        return;    
        if(flag==true)      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            return;         //!!!!要点:只要有一种 符合条件,就可以结束所有的递归!!!! 
        if((abs(x-x1)+abs(y-y1))%2!=(t-k)%2)
            return;         //!!!!要点:由奇偶性剪枝,否则超时 !!!!     
        if(abs(x-x1)+abs(y-y1)>t-k)
            return;         //减时操作:最短路径的时间不能超过剩余时间    
                    
        if(k==t)
            {
                if(a[x1][y1]=='D')
                    flag=true;
                return;
            }
            
        vis[x1][y1]=0;
        for(int i=0;i<4;i++)
            {
                if(vis[x1+b[i]][y1+c[i]])
                    {
                        x1+=b[i];y1+=c[i];
                        dp(k+1);
                        x1-=b[i];y1-=c[i];
                    }   // for循环内回溯,防止对另一种走法产生干扰。   
            }
        vis[x1][y1]=1;  // 外部标记回溯 
    
    }
    
    int main()
    {
        while(cin>>n>>m>>t)
            {
                int sum=0;
                flag=false;
                if(n==0&&m==0&&t==0)
                    break;
                memset(vis,0,sizeof(vis));
                for(int i=0;i<n;i++)
                    {
                        scanf("%s",a[i]);   //逐行输入,无&,千万不能scanf( "%c",&a[i][j])
                    }                       //scanf %c 能读取空格和换行符。     
                for(int i=0;i<n;i++)
                    {
                        for(int j=0;j<m;j++)
                            {
                                if(a[i][j]=='S')
                                    {
                                        x1=i;
                                        y1=j;
                                    }
                                if(a[i][j]=='D')
                                    {
                                        x=i;
                                        y=j;
                                        vis[i][j]=1;
                                        sum++;
                                    }
                                if(a[i][j]=='.')
                                    {
                                        vis[i][j]=1;
                                        sum++;
                                    }
                            }
                    }
                if(sum<t)    //减时操作:可走方格不能少于固定时间 
                    {
                        printf("NO
    ");
                        continue;
                    }
                dp(0);  
                if(flag==true)
                    printf("YES
    ");
                else
                    printf("NO
    ");
            }
        return 0;
    } 
  • 相关阅读:
    springcloud相关组件使用时的jar包
    day62-django-反向解析、路由分发、名称空间、伪静态、视图层(三板斧、JsonResponse、form表单上传文件、request对象方法、FBV与CBV)
    day61-django-数据的查改删、创建表关系 、请求生命周期流程图、路由层(路由匹配 无名分组 有名分组 无名有名是否可以混合使用 反向解析)
    AcWing487. 金明的预算方案题解(DP,分组背包)
    day60-django-静态文件配置、request方法、链接数据库、ORM操作
    day59-django-写一个简易版本的web框架、jinja2、web框架请求流程图、框架介绍、django基本操作
    day58-jQuery事件的阻止、委托、页面加载、动画、前端框架bootstrap、搭建图书管理系统
    day57-jQuery练习、操作标签、事件
    day56-js原生事件绑定-jQuery导入、查找标签
    day55-前端js-BOM与DOM操作
  • 原文地址:https://www.cnblogs.com/biggan/p/7434983.html
Copyright © 2011-2022 走看看