zoukankan      html  css  js  c++  java
  • TZOJ 1221 Tempter of the Bone(回溯+剪枝)

    描述

    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.

    输入

    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.

    输出

    For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

    样例输入

    4 4 5
    S.X.
    ..X.
    ..XD
    ....
    3 4 5
    S.X.
    ..X.
    ...D
    0 0 0

    样例输出

    NO
    YES

    题意

    给你一个n*m大小的矩阵,判断老鼠能否刚好在T步后到达出口(‘.’是只能经过一次的路,K步后出口打开)

    题解

    一个经典的回溯题,暴力写完后提交TLE,后来发现需要剪枝

    最短路minstep=abs(s.x-e.x)+abs(s.y-e.y),最长路maxstep=n*m-1;

    可以想到一个简单的剪枝,如果老鼠从起点开始,最短路minstep>t直接NO,或者最长路maxstep<t直接NO,写完后提交,又TLE,还要剪枝

    通过多次画图可以发现,

    0,1,0,1,0 

    1,0,1,0,1

    0,1,0,1,0

    1,0,1,0,1

    如果起点和终点不同(0->1)(1->0),那么,要经过奇数步才能到达终点

    如果起点和终点相同(0->0)(1->1),那么,需经过偶数步才能到达终点

    那就很明显了,需要奇偶剪枝,(t-minstep)%2==1直接NO,在回溯的时候也可以用,大大降低了不必要的运算

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 char G[10][10];
     4 inline int Abs(int a){return a>0?a:-a;}
     5 struct p
     6 {
     7     int x,y;
     8 }s,e;
     9 int dx[]={0,0,1,-1};
    10 int dy[]={1,-1,0,0};
    11 int n,m,t,flag,minstep;
    12 void dfs(int x,int y,int k)
    13 {
    14     if(x==e.x&&y==e.y&&k==0)flag=1;
    15     minstep=Abs(x-e.x)+Abs(y-e.y);
    16     if(minstep>k||(k-minstep)%2)return;
    17     if(!flag)
    18     {
    19         for(int i=0;i<4;++i)
    20         {
    21             int xx=x+dx[i];
    22             int yy=y+dy[i];
    23             if(xx>=0&&xx<n&&yy>=0&&yy<m&&G[xx][yy]!='X')
    24             {
    25                 G[xx][yy]='X';
    26                 dfs(xx,yy,k-1);
    27                 G[xx][yy]='.';
    28             }
    29         }
    30     }
    31 }
    32 int main()
    33 {
    34     while(scanf("%d%d%d",&n,&m,&t)!=EOF,n||m||t)
    35     {
    36         flag=0;
    37         for(int i=0;i<n;++i)
    38         {
    39             scanf("%s",G[i]);
    40             for(int j=0;j<m;++j)
    41             {
    42                 if(G[i][j]=='S')
    43                     s.x=i,s.y=j;
    44                 else if(G[i][j]=='D')
    45                     e.x=i,e.y=j;
    46             }
    47         }
    48         minstep=Abs(s.x-e.x)+Abs(s.y-e.y);
    49         if(n*m-1<t||minstep>t||(t-minstep)%2){printf("NO
    ");continue;}
    50         G[s.x][s.y]='X';
    51         dfs(s.x,s.y,t);
    52         printf("%s
    ",flag?"YES":"NO");
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
    LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
    LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium
    Cellular Traffic Offloading
    在word 2010中采用EndNote X7插入引用
    屏幕截图和标记
    A Nice Paper About Mobile Data Offloading
    linux Redhat 6环境上通过源码包安装DRBD 8
    hbase shell中执行list命令报错:ERROR: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing
    如何通过phoenix中查看表的主键信息
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8724531.html
Copyright © 2011-2022 走看看