#include <bits/stdc++.h>
using namespace std;
constexpr int d[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
int n, m, c;
inline constexpr bool oob(int x, int y) {
return x < 0 || y < 0 || x >= n || y >= m;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
int dp[n][m];
bool vis[n][m] = {};
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
char x;
cin >> x;
if (x == '.') dp[i][j] = 0;
else if (x == 'F') dp[i][j] = 1;
else dp[i][j] = 2;
}
queue<pair<int, int>> p, q;
int now = dp[0][0];
q.emplace(0, 0);
vis[0][0] = true;
while (!q.empty()) {
p.swap(q);
++c;
while (!p.empty()) {
auto [x, y] = p.front();
p.pop();
for (auto &k : d) {
int x1 = x + k[0], y1 = y + k[1];
if (oob(x1, y1) || vis[x1][y1]) continue;
vis[x1][y1] = true;
if (dp[x1][y1] == now) p.emplace(x1, y1);
else if (dp[x1][y1]) q.emplace(x1, y1);
}
}
now = 3 - now;
}
cout << c;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |