zoukankan      html  css  js  c++  java
  • Tempter of the Bone

                                                   Tempter of the Bone

                                           Time Limit: 1000ms                                            Memory Limit: 32768KB
                                                            64-bit integer IO format:      Java class name:
     
    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


    题意:
    ”S“为起点,”D“为终点,”X“为塌陷的地板。
    每走一步为1秒,只要走到下一块地板,原来的地板便立即塌陷,判断从起点到终是否可以刚好t秒。

    思路分析:
    直接用深搜会超时,所以要进行简单的剪枝,这里就需要了解奇偶剪枝。

    奇偶剪枝
    s        
    |        
    |        
    |        
    + e
    
    

                设所在位置s (sx,sy) 与 目标位置 e(ex,ey)

                易知最短步数step=abs(ex-sx)+abs(ey-sy)

                故 abs(ex-sx)+abs(ey-sy)的奇偶性就确定了最短步数step的奇偶性

                绕过一个有障碍的格子,需要多走两步,如果经过t步刚好到终点,则必定有t-step=偶数;

                因此 只有t-abs(ex-sx)-abs(ey-sy)=偶数(也可以写成t+ex+sx+ey+sy=偶数), 才能从起点刚好经过t步到达终点。

    源代码:

     1 #include <cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 const int MAX=10;
     5 int n,m,t;
     6 char a[MAX][MAX];
     7 int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
     8 int startx,starty,endx,endy,flag;
     9 void dfs(int dx,int dy,int k)
    10 {
    11     int i;
    12     if(flag)return;
    13     if(a[dx][dy]=='X') return;
    14     if(dx==endx&&dy==endy&&k==t)
    15     {
    16         flag=1;
    17         return;
    18     }
    19     if(dx<1||dx>n||dy<1||dy>m||k>t) return;
    20     for(i=0;i<4;i++)
    21     {
    22         a[dx][dy]='X';
    23         dfs(dx+d[i][0],dy+d[i][1],k+1);  //这个地方必须用k+1,不然当前一个不达到条件而返回时,k却已经加1
    24         a[dx][dy]='.';
    25         if(flag)return;
    26     }
    27 }
    28 int main()
    29 {
    30 
    31     while(scanf("%d%d%d",&n,&m,&t)&&n&&m&&t)
    32     {
    33         flag=0;
    34         int s=0;
    35         for(int i=1;i<=n;i++)
    36             for(int j=1;j<=m;j++)
    37             {
    38                cin>>a[i][j];      //如果这里用scanf的话,会吃掉每一行的换行符,
    39                 if(a[i][j]=='S')
    40                 {
    41                     startx=i;
    42                     starty=j;
    43                     continue;
    44                 }
    45                 if(a[i][j]=='D')
    46                 {
    47                     endx=i;
    48                     endy=j;
    49                     continue;
    50                 }
    51                 if(a[i][j]=='X')
    52                     s++;
    53         }
    54         if((t+startx+starty+endx+endy)%2==1)    //奇偶剪枝
    55         {  cout<<"NO"<<endl; continue;}
    56         if((m*n-s)<t)
    57         {  cout<<"NO"<<endl; continue;}
    58         dfs(startx,starty,0);
    59         if(flag)
    60           cout<<"YES"<<endl;
    61         else
    62            cout<<"NO"<<endl;
    63     }
    64     return 0;
    65 }
     
  • 相关阅读:
    OpenCV2:幼儿园篇 第七章 界面事件
    还有一百篇博客
    OpenCV2:幼儿园篇 第五章 图像几何变换
    禁止切换全半角
    AP模式(路由器的几种模式)
    Xcode 8.0注释的问题
    苹果机子(将不断更新)
    App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.Temporary exceptions can be configured via your app's Info.plist file.
    No matching provisioning profiles found:No provisioning profiles with a valid signing idea....没有找到匹配的配置概要文件:没有配置概要文件与一个有效的签名
    TableView的快捷使用(5分钟demo)深入前往Json和Xml解析
  • 原文地址:https://www.cnblogs.com/q-c-y/p/5410193.html
Copyright © 2011-2022 走看看