zoukankan      html  css  js  c++  java
  • zoj 2110 骨头的诱惑(Tempter of the Bone) DFS

    地址  https://vjudge.net/problem/ZOJ-2110

     

    初始看就是个简单的DFS 或者BFS

    后来卡了半天 才发现 是恰好T秒达到.......

    做了剪枝 修改了成功退出的判断条件为 == T 然后ac了

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 #include <string>
     5 #include <memory.h>
     6 
     7 
     8 using namespace std;
     9 
    10 int n, m, t;
    11 
    12 const int MAX_SIZE = 90;
    13 
    14 char G[MAX_SIZE][MAX_SIZE];
    15 
    16 int currx, curry;
    17 
    18 int addx[] = { 0,0,1,-1 };
    19 int addy[] = { 1,-1,0,0 };
    20 
    21 int endX;
    22 int endY;
    23 
    24 
    25 
    26 int Dfs(int x, int y, int limit)
    27 {
    28     int count = 0;
    29 
    30     if (x < 0 || x >= n || y < 0 || y >= m) {
    31         return 0;
    32     }
    33     if (G[x][y] == 'X')
    34         return 0;
    35     if (G[x][y] == 'D' && limit == t)
    36         return 1;
    37     if (limit > t)
    38         return 0;
    39 
    40     int temp = (t - limit) - abs(x - endX) - abs(y - endY);
    41     
    42     if (temp < 0 )
    43         return 0;
    44 
    45 
    46     for (int i = 0; i < 4; i++) {
    47         int newx = addx[i] + x;
    48         int newy = addy[i] + y;
    49         char oldV = G[x][y];
    50         G[x][y] = 'X';
    51         if (Dfs(newx, newy, limit + 1) == 1)
    52             return 1;
    53         G[x][y] = oldV;
    54     }
    55 
    56     return 0;
    57 }
    58 
    59 
    60 int main()
    61 {
    62     while (scanf("%d%d%d", &n, &m, &t)) {
    63         if (n == m && m== t && t == 0) 
    64             break;
    65 
    66         memset(G, 0, sizeof G);
    67 
    68         for (int i = 0; i < n; i++)
    69             for (int j = 0; j < m; j++) {
    70                 cin >> G[i][j];
    71                 if (G[i][j] == 'S') {
    72                     currx = i;
    73                     curry = j;
    74                 }
    75                 else if (G[i][j] == 'D') {
    76                     endX = i;
    77                     endY = j;
    78                 }
    79             }
    80         if (Dfs(currx, curry, 0) != 0)
    81             printf("YES
    ");
    82         else
    83             printf("NO
    "); 
    84     }
    85 
    86     return 0;
    87 }
    View Code
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    C++内存管理
    GitHub 简单用法
    Tembin
    git
    js 插件使用总结
    cas sso
    Redis实战
    全面分析 Spring 的编程式事务管理及声明式事务管理
    mybatis
    b2b
  • 原文地址:https://www.cnblogs.com/itdef/p/12518883.html
Copyright © 2011-2022 走看看