Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <cstring>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int point = 2147483647, x, y;
- bool dfs (vector<string> labirin, int i, int j, int poin) {
- if (i < 0 || j < 0 || i >= y || j >= x) return false;
- if (labirin[i][j] == '#') return false;
- if (labirin[i][j] == 'P') {
- if (poin < point) point = poin;
- return true;
- }
- labirin[i][j] = '#';
- bool b = false;
- b |= dfs(labirin, i + 1, j, poin + 1);
- b |= dfs(labirin, i, j + 1, poin + 1);
- b |= dfs(labirin, i - 1, j, poin + 1);
- b |= dfs(labirin, i, j - 1, poin + 1);
- return b;
- }
- int main() {
- cin >> y >> x;
- int m = 0, n = 0;
- vector<string> v(y);
- for (int i = 0; i < y; i++) {
- cin >> v[i];
- for (int j = 0; j < x; j++) {
- if (v[i][j] == 'B') {
- m = i;
- n = j;
- }
- }
- }
- bool b = dfs(v, m, n, 0);
- if (b) cout << point;
- else cout << -1;
- }
Add Comment
Please, Sign In to add comment