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

    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
     

    Recommend
    JGShining

    思路:dfs,T了很多次,WA了很多次,开始没看见那个exactly T-ths - -||,很慢啊啊啊!

     1 #include <cstdio>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 
     6 const int dx[]={0,0,1,-1};
     7 const int dy[]={1,-1,0,0};
     8 int n,m,t,xx,yy,ax,ay,tt=0,fc,cnt;
     9 char map[10][10];
    10 bool f[10][10],flag;
    11 
    12 void dfs(int x,int y)
    13 {
    14 int xx,yy;
    15 //printf("x:%d y:%d tt:%d \n",x,y,tt);
    16     for (int i=0;i<4;i++)
    17     {
    18         xx=dx[i]+x;
    19         yy=dy[i]+y;
    20       if (map[xx][yy]=='D') 
    21       {
    22     //        printf("tt:%d\n",tt);
    23             if (tt+1==t)
    24             {
    25                 flag=true;
    26                 return;
    27             }
    28     //      return;
    29       }
    30         if (xx>=0 && xx<n && yy>=0 && yy<m && not f[xx][yy] && map[xx][yy]=='.' && not flag && tt<=t) //&& tt<fc && tt<t && not flag)
    31         {
    32             f[xx][yy]=true;
    33             tt++;
    34             dfs(xx,yy);
    35             tt--;
    36         //    printf("---------------------\n");
    37             f[xx][yy]=false;
    38         }
    39     }
    40 }
    41 
    42 
    43 void init()
    44 {
    45     while (scanf("%d%d%d",&n,&m,&t)!=EOF)
    46     {
    47         cnt=0;
    48         if (n==0 && m==0 && t==0) break;
    49         for (int i=0;i<n;i++)
    50         {
    51             scanf("%s",map[i]);
    52             for (int j=0;j<m;j++)
    53             {
    54                 if (map[i][j]=='S')
    55                 {
    56                     ax=i;
    57                     ay=j;
    58                 }
    59                 if (map[i][j]=='.')
    60                     cnt++;
    61             }
    62         }
    63         memset(f,false,sizeof(f));
    64         flag=false;
    65         f[ax][ay]=true;
    66         tt=0;
    67         fc=0x3f3f3f;
    68         if (cnt+1>=t)
    69         dfs(ax,ay);
    70         if (flag==true)
    71             printf("YES\n");
    72         else printf("NO\n");
    73     }
    74 }
    75 
    76 int main ()
    77 {
    78 init();
    79 
    80 return 0;
    81 }
  • 相关阅读:
    Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置
    python 对数函数
    Python使用os.listdir()函数来得目录内容的介绍
    Linux下基于HTTP协议带用户认证的GIT开发环境设置
    在python中如何设置当前工作目录
    Python 获得命令行参数的方法
    Python time mktime()方法
    linux中怎样从底部向上查看log文件
    python基础之使用os.system来执行系统命令
    python datetime处理时间
  • 原文地址:https://www.cnblogs.com/cssystem/p/2836583.html
Copyright © 2011-2022 走看看