• HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)


    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
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 using namespace std;
     6 int T,si,sj,di,dj,n,m;
     7 int tx[]={1,-1,0,0};
     8 int ty[]={0,0,-1,1};
     9 char a[105][105];
    10 bool flag;
    11 void dfs(int x,int y,int t)
    12 {
    13     int i,xx,yy;
    14     if(x==di&&y==dj&&t==T)//不能写成map[i][j]=='D'因为D会被改掉
    15         flag=true;
    16     if(flag)
    17        return ;
    18     if((abs(di-x)+abs(dj-y))%2!=(T-t)%2)//如果需要走的步数和差的时间奇偶性不同不可能走出
    19        return ;
    20     for(i=0;i<4;i++)
    21     {
    22         xx=x+tx[i];
    23         yy=y+ty[i];
    24         if(xx>=0&&yy>=0&&xx<n&&yy<m&&a[xx][yy]!='X')
    25         {
    26             a[xx][yy]='X';
    27             dfs(xx,yy,t+1);
    28             a[xx][yy]='.';
    29         }
    30     }
    31 }
    32 int main()
    33 {
    34     int i,j;
    35     while(cin >> n>>m>>T&&m&&n&&T)
    36     {
    37         int num=0;
    38         flag=false;
    39         for(i=0;i<n;i++)
    40            for(j=0;j<m;j++)
    41            {
    42               cin >> a[i][j];
    43               if(a[i][j]=='S')
    44               {
    45                   si=i;
    46                   sj=j;
    47               }
    48               else if(a[i][j]=='D')
    49               {
    50                   di=i;
    51                   dj=j;
    52                   num++;
    53               }
    54               else if(a[i][j]=='.')
    55                 num++;
    56            }
    57            a[si][sj]='X';
    58         if(num>=T)//如果总的能走的路比时间小,不可能出去
    59            dfs(si,sj,0);
    60         if(flag)
    61            cout << "YES"<<endl;
    62         else
    63            cout << "NO" << endl;
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    ClientDataSet 心得
    TClientDataSet中关于TField、TFieldDef动态创立字段的应用
    Delphi CxGrid 汇总(4)
    Delphi CxGrid 汇总(3)
    Delphi CxGrid 汇总(2)
    Delphi cxGrid使用汇总(一)
    修改后的SQL分页存储过程,利用2分法,支持排序
    字符串操作之格式化
    关于 cxGrid 的过滤问题
    cxGrid实现取消过滤和排序后定位到首行(单选和多选)
  • 原文地址:https://www.cnblogs.com/Annetree/p/5639091.html
Copyright © 2020-2023  润新知