zoukankan      html  css  js  c++  java
  • 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): 94669    Accepted Submission(s): 25669


    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
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <queue>
     4 #include <cstring>
     5 #include <algorithm>
     6 #define LL long long
     7 using namespace std;
     8 char map[10][10];
     9 bool vis[10][10];
    10 int dx[]={0,0,-1,1},dy[]={1,-1,0,0};
    11 int n,m,t;
    12 int sx,sy,ex,ey;
    13 bool flag;
    14 bool dfs(int x,int y,int tim)
    15 {
    16     if(flag)    return 1;
    17     if(x<0||y<0||x>=n||y>=m||vis[x][y]==1||tim>t||map[x][y]=='X')
    18         return 0;
    19     if(tim<t&&map[x][y]=='D')    return 0;
    20     int w=abs(x-ex)+abs(y-ey);
    21     if(t-tim<w)    return 0;
    22     int tem=w-abs(t-tim);
    23     if(tem>0||tem%2)    return 0;
    24     if(tim==t&&map[x][y]=='D')    {flag=1;return 1;}
    25     for(int i=0;i<4;i++)
    26     {
    27         vis[x][y]=1;
    28         int nx=x+dx[i];
    29         int ny=y+dy[i];
    30         dfs(nx,ny,tim+1);
    31         vis[x][y]=0;
    32     }
    33     return 0;
    34 }
    35 int main()
    36 {
    37     freopen("in.txt","r",stdin);
    38     while(scanf("%d%d%d",&n,&m,&t))
    39     {
    40         getchar();
    41         int tim=0,z=0;
    42         flag=0;
    43         if(n==0&&m==0&&t==0)    break;
    44         for(int i=0;i<n;i++)
    45         {
    46             for(int j=0;j<m;j++)
    47             {
    48                 scanf("%c",&map[i][j]);
    49                 if(map[i][j]=='S')
    50                 {
    51                     sx=i,sy=j;
    52                 }
    53                 if(map[i][j]=='D')
    54                 {
    55                     ex=i,ey=j;
    56                 }
    57             //    if(map[i][j]=='X')    z++;
    58             }
    59             getchar();
    60         }
    61         //if(n*m-z<t){printf("NO
    ");continue;}
    62         memset(vis,0,sizeof(vis));
    63         dfs(sx,sy,0);
    64         if(flag)    printf("YES
    ");
    65         else    printf("NO
    ");
    66     }
    67 }
  • 相关阅读:
    EF 简单的 CRUD、分页 代码笔记
    C#中的正则 Regex类
    动态字符串 Stringbuilder类
    C# 字符串操作
    集合简单总结 ArrayList、List、Hashtable、Dictionary
    C#面向对象2 静态类、静态成员的理解
    C#面向对象1 类 以及 类的继承(new、ovverride)
    无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer、Windows服务器管理工具或NET START命令启动它
    WebClient上传音频文件
    发布网站详细步骤(.Net)
  • 原文地址:https://www.cnblogs.com/a1225234/p/5018112.html
Copyright © 2011-2022 走看看