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

    Tempter of the Bone

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 <cstdlib>
     2 #include <cstdio>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <cmath>
     6 
     7 using namespace std;
     8 
     9 char map[8][8];
    10 bool escape;
    11 int di, dj, m, n, t;
    12 int dir[4][2] = {{0, -1}, {0, 1}, {1,0},{-1,0}};
    13 
    14 void dfs(int si, int sj, int cnt)
    15 {
    16     int i, temp;
    17     if (si > n || sj > m || si <= 0 || sj <= 0) return;
    18     if (si == di && sj == dj && cnt == t){escape = 1; return;}
    19     temp = (t-cnt) - (fabs(si-di) + fabs(sj-dj));
    20     if (temp<0 || temp%2) return;
    21     for (i = 0; i < 4; ++i)
    22     {
    23         if (map[si+dir[i][0]][sj+dir[i][1]] != 'X')
    24         {
    25             map[si+dir[i][0]][sj+dir[i][1]] = 'X';
    26             dfs(si+dir[i][0], sj+dir[i][1], cnt+1);
    27             if (escape) return;
    28             map[si+dir[i][0]][sj+dir[i][1]] = '.';
    29         }
    30     }
    31     return;
    32 }
    33 
    34 int main(void)
    35 {
    36     int i, j, si, sj;
    37 #ifndef ONLINE_JUDGE
    38     freopen("in", "r", stdin);
    39 #endif
    40     while (~scanf("%d%d%d", &n, &m, &t))
    41     {
    42         if (!n && !m && !t) break;
    43         int wall = 0;
    44         getchar();
    45         for (i = 1; i < n + 1; ++i)
    46         {
    47             for (j = 1; j < m + 1; ++j)
    48             {
    49                 scanf("%c", &map[i][j]);
    50                 if (map[i][j] == 'S') si=i,sj=j;
    51                 else if (map[i][j] == 'D') di=i,dj=j;
    52                 else if (map[i][j]=='X') wall++;
    53             }
    54             getchar();
    55         }
    56         if (n*m - wall <= t){printf("NO\n"); continue;}
    57         escape = 0;
    58         map[si][sj] = 'X';
    59         dfs(si, sj, 0);
    60         if (escape) printf("YES\n");
    61         else printf("NO\n");
    62     }
    63 
    64     return 0;
    65 }

    入门做的第一道搜索题,注意两个地方的剪枝。其实上面的代码是抄的书上的,还好,看懂了,以后自己写。

    还得谢谢xrl,dfs,bfs刚开始看各种不懂,,后来xrl跟我说了这本书,又给我讲了一下,我才知道代码怎么写。

  • 相关阅读:
    FireFox/Chrome不支持在前台js读后台Attributes.Add("DefaultValue")的属性
    IIS Web怪问题: Access is denied due to invalid credentials.
    转:OWC学习笔记电子表格(Spreadsheet)风格属性设置
    最全的CSS浏览器兼容问题整理(IE6.0、IE7.0 与 FireFox)
    转:[网站安全]避免在站点中通过URL参数做重定向
    [SQL Server 2005 BI]在.NET中创建SQL 2005 KPI
    即使asp:TreeView有几万个节点,也让IE不死的解决方法
    Visual Totals in MDX and Role Security
    css hack
    让Updatepanel中的控件触发整个页面Postback
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2794526.html
Copyright © 2011-2022 走看看