zoukankan      html  css  js  c++  java
  • hdu 1010 Tempter of the Bone(dfs)

    Tempter of the Bone

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


    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
     
    Recommend
    JGShining   |   We have carefully selected several similar problems for you:  1072 1015 1372 1258 1239 
     
    很好的搜索,需要强大的剪枝避免超时,参考了网上大神的解释。
    第一个剪枝我们可以想到,当剩下的步数大于剩下的时间的时候,狗是不能走到的;
     
    接下来我们来第二个剪枝:
    我们把map的奇偶性以01编号:
    0 1 0 1 0 1 
    1 0 1 0 1 0 
    0 1 0 1 0 1 
    1 0 1 0 1 0 
    0 1 0 1 0 1 
    我们发现从0走一步一定走到1,从1走一步一定走到0。
    也就是说,如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。
    同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。
     
    也就是说,狗的坐标x、y和对2取余是它的奇偶性,Dxy和对2取余是D的奇偶性。
    两个奇偶性一加再对2取余,拿这个余数去与剩下时间对2取余的余数作比较即可。
     
    题意:输入一个n*m的矩阵以及时间T。矩阵中有'X':墙,不能走;'.':通路,可以走;'S':开始点。'D':门,结束点。要求从开始点开始走,每秒一步,走过的路不能再走,走到D的时候正好花费时间T。输出“YES”或“NO”表示能否在时间T的时候走到D。
     
    附上代码:
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 int n,m,T;
     6 char map[10][10],fail,a2,b2;
     7 int visit[10][10];
     8 int f[4][2]= {1,0,-1,0,0,1,0,-1};
     9 int abs(int a)  //绝对值函数
    10 {
    11     return a>=0?a:-a;
    12 }
    13 void DFS(int x,int y,int k)
    14 {
    15     int i,nx,ny;
    16     if(x==a2&&y==b2)   //无论是否时间刚好满足题意,此时都需要返回上一层
    17     {
    18         if(k==T)    //时间相等
    19             fail=1;
    20         return;
    21     }
    22     if(k>=T)   //超过时间的剪枝
    23         return;
    24     for(i=0; i<4; i++)
    25     {
    26         nx=x+f[i][0];
    27         ny=y+f[i][1];
    28         if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&map[nx][ny]!='X'&&!visit[nx][ny])
    29         {
    30             visit[nx][ny]=1;
    31             DFS(nx,ny,k+1);
    32             visit[nx][ny]=0;
    33             if(fail) return;
    34         }
    35     }
    36 }
    37 int main()
    38 {
    39     int i,j,a1,b1;
    40     while(~scanf("%d%d%d",&n,&m,&T))
    41     {
    42         if(n==0&&m==0&&T==0)
    43             break;
    44         for(i=1; i<=n; i++)
    45         {
    46             for(j=1; j<=m; j++)
    47             {
    48                 cin>>map[i][j];
    49                 if(map[i][j]=='S')   //记录起点位置
    50                 {
    51                     a1=i;
    52                     b1=j;
    53                 }
    54                 else if(map[i][j]=='D')  //终点位置
    55                 {
    56                     a2=i;
    57                     b2=j;
    58                 }
    59             }
    60         }
    61         if((abs(a2-a1)+abs(b2-b1)>T)||((a1+a2+b1+b2+T)%2==1))  //剪枝,步数不够和奇偶剪枝
    62         {
    63             printf("NO
    ");
    64             continue;
    65         }
    66         memset(visit,0,sizeof(visit));  //标记步数是否走过
    67         visit[a1][b1]=1;
    68         fail=0;
    69         DFS(a1,b1,0);
    70         if(fail)  printf("YES
    ");
    71         else  printf("NO
    ");
    72     }
    73     return 0;
    74 }
  • 相关阅读:
    Sql Server 邮件日志 操作 IT
    导出Excel IT
    Sqlserver 2005 修改数据库默认排序 IT
    SqlServer 备份数据库语法 IT
    HDFS常用shell命令
    改写UMFPACK算例中的压缩方式(动态)
    umFPACK使用调用(一)
    改写UMFPACK算例中的压缩方式(静态)
    利用C/C++实现从文件读入到子程序中调用返回结果
    改写UMFPACK算例中的压缩方式
  • 原文地址:https://www.cnblogs.com/pshw/p/4760452.html
Copyright © 2011-2022 走看看