• Tempter of the Bone 搜索---奇偶性剪枝


    Tempter of the Bone

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 5   Accepted Submission(s) : 1
    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
    ZJCPC2004
     
    在T时刻,到达。
     
     
     
    详细的看代码。开始做的时候,一直超时。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 using namespace std;
     6 
     7 int n,m,T,sx,sy,dx,dy;
     8 char a[10][10];
     9 bool vis[10][10],Glag;
    10 int map1[4][2]={ {1,0},{0,1},{-1,0},{0,-1} };
    11 
    12 
    13 void dfs(int x,int y,int cur)
    14 {
    15     int i,x1,y1;
    16     if(cur==T && x==dx && y==dy)
    17     {
    18         Glag=true;
    19         return;
    20     }
    21     if(Glag==true) return;
    22     int t,s;
    23     t=T-cur;
    24     s=(int)abs(dx-x)+(int)abs(dy-y);//剩余的时间 t - 剩余的步数 s
    25                                     //如果,t是奇数,s是奇数。那么t-s是偶数。
    26                                     //如果,t是偶数,s是奇数。那么t-s是奇数。
    27                                     //所以只要判断 相减后是否>0 && 偶数。
    28     t=t-s;
    29     if(t<0||(t&1)==1) return;//奇偶性剪枝
    30 
    31     for(i=0;i<4;i++)
    32     {
    33         x1=x+map1[i][0];
    34         y1=y+map1[i][1];
    35         if(x1>=1&&x1<=n && y1>=1&&y1<=m && vis[x1][y1]==false && a[x1][y1]!='X')
    36         {
    37             vis[x1][y1]=true;
    38             dfs(x1,y1,cur+1);
    39             vis[x1][y1]=false;
    40         }
    41     }
    42 }
    43 
    44 int main()
    45 {
    46     int i,j,wall;
    47     while(scanf("%d%d%d",&n,&m,&T)>0)
    48     {
    49         if(n==0&&m==0&&T==0)break;
    50         for(i=1;i<=n;i++)
    51         scanf("%s",a[i]+1);
    52 
    53         wall=0;
    54         for(i=1;i<=n;i++)
    55         for(j=1;j<=m;j++)
    56         if(a[i][j]=='S')
    57         {
    58             sx=i;
    59             sy=j;
    60         }
    61         else if(a[i][j]=='D')
    62         {
    63             dx=i;
    64             dy=j;
    65         }
    66         else if(a[i][j]=='X')
    67         wall++;
    68 
    69         if(n*m-1-wall<T)//这也是一个优化,T太大了,根本走不到。
    70         {
    71             printf("NO
    ");
    72             continue;
    73         }
    74         Glag=false;
    75         memset(vis,false,sizeof(vis));
    76         vis[sx][sy]=true;
    77         dfs(sx,sy,0);
    78         if(Glag==true)
    79             printf("YES
    ");
    80         else printf("NO
    ");
    81     }
    82     return 0;
    83 }
     
     
  • 相关阅读:
    Cassandra vs. HBase
    游戏留存率分析
    Writing a Discard Server
    t
    启动进程 派生 关闭而不关闭
    单页应用 SAP Vue
    TiDB 整体架构 结合yarn zookeeper分析架构
    Writing a Simple YARN Application 从hadoop生态抽出yarn ,单独使用yarn
    Phoenix put the sql back in NoSql
    事件序列化器 Flume 的无数据丢失保证,Channel 和事务
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3391689.html
Copyright © 2020-2023  润新知