Yesver08

Selamatkan Putri

Feb 15th, 2021 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int point = 2147483647, x, y;
  10.  
  11. bool dfs (vector<string> labirin, int i, int j, int poin) {
  12.     if (i < 0 || j < 0 || i >= y || j >= x) return false;
  13.     if (labirin[i][j] == '#') return false;
  14.     if (labirin[i][j] == 'P') {
  15.         if (poin < point) point = poin;
  16.         return true;
  17.     }
  18.     labirin[i][j] = '#';
  19.     bool b = false;
  20.     b |= dfs(labirin, i + 1, j, poin + 1);
  21.     b |= dfs(labirin, i, j + 1, poin + 1);
  22.     b |= dfs(labirin, i - 1, j, poin + 1);
  23.     b |= dfs(labirin, i, j - 1, poin + 1);
  24.     return b;
  25. }
  26.  
  27. int main() {
  28.     cin >> y >> x;
  29.     int m = 0, n = 0;
  30.     vector<string> v(y);
  31.     for (int i = 0; i < y; i++) {
  32.         cin >> v[i];
  33.         for (int j = 0; j < x; j++) {
  34.             if (v[i][j] == 'B') {
  35.                 m = i;
  36.                 n = j;
  37.             }
  38.         }
  39.     }
  40.     bool b = dfs(v, m, n, 0);
  41.     if (b) cout << point;
  42.     else cout << -1;
  43. }
  44.  
Add Comment
Please, Sign In to add comment