zoukankan      html  css  js  c++  java
  • HOJ3237----BFS/DFS

     1 /*
     2 注意两点
     3 1. 不可以使用替换可用节点为不可用节点的方法进行DFS
     4     因为角落也可能有油,替换了就出不来。(某学长指导)
     5 2. 可用通过开一个数组(例如我的b[][]数组)
     6     用了存储到当前位置剩余最大油量
     7     这样到话,下次若有更优解,则更新b
     8     反之无需继续遍历
     9 对于BFS,可用结构体记录坐标和到当前位置到剩余油量
    10 用方法2剪枝即可
    11 以下为DFS方法实现
    12 *13 #include <cstring>
    14 #include <iostream>
    15 using namespace std;
    16 int n, m, l;
    17 char a[105][105];
    18 int b[105][105];
    19 int w[4][2] = {{0, 1}, {1, 0}, { -1, 0}, {0, -1}};
    20 bool ans;
    21 void dfs(int x, int y, int s)
    22 {
    23     //cout << "x = " << x+1 << "  y = " << y+1 << "  s = " << s <<"  a = " << a[x][y] << endl;
    24     if (x == n - 1 && y == m - 1) {ans = true; return;}
    25     if (b[x][y] >= s) return;
    26     if (a[x][y] == '+') s = l;
    27     if (s <= 0 || ans) return;
    28     b[x][y] = s;
    29 
    30     for (int i = 0; i < 4; ++i)
    31     {
    32         int nx = x + w[i][0], ny = y + w[i][1];
    33         if (nx >= 0 && ny >= 0 && nx < n && ny < m && a[nx][ny] != '#')
    34             dfs(nx, ny, s-1);
    35     }
    36 }
    37 int main()
    38 {
    39     int t;
    40     cin >> t;
    41     while (t--)
    42     {
    43         memset(b, -1, sizeof b);
    44         cin >> n >> m >> l;
    45         for (int i = 0; i < n; ++i)
    46             cin >> a[i];
    47         ans = false;
    48         dfs(0, 0, l);
    49         if (ans) cout << "Yes" << endl;
    50         else cout << "No" << endl;
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    从零开始学SQLSERVER-UNION
    从零开始学SQLSERVER-BETWEEN
    从零开始学SQLSERVER-LIKE
    从零开始学SQLSERVER-存储过程(基础用法)
    从零开始学SQLSERVER-TOP
    从零开始学SQLSERER-INNER JOIN
    从零开始学SQLSERVER-DELECT(删除)
    从零开始学SQLSERVER-ORDER BY(排序)
    从零开始学SQLSERVER-WHERE
    使用 C# 9 的records作为强类型ID
  • 原文地址:https://www.cnblogs.com/qq188380780/p/7271324.html
Copyright © 2011-2022 走看看