This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pint;
const int D[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int h, w, grid[1000][1000];
bool seen[1000][1000];
queue<pint> q[3];
int main() {
cin >> h >> w;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
char c;
cin >> c;
if (c == 'T') grid[i][j] = 1;
else if (c == 'B') grid[i][j] = 2;
}
}
int n = 0;
q[grid[0][0]].emplace(0, -1);
for (int type = grid[0][0]; not q[type].empty(); ++n, type = 3 - type) {
while (not q[type].empty()) {
auto [x, y] = q[type].front();
q[type].pop();
for (int i = 0; i < 4; ++i) {
int x1 = x + D[i][0], y1 = y + D[i][1];
if (x1 >= 0 and x1 < h and y1 >= 0 and y1 < w) {
if (not seen[x1][y1]) {
seen[x1][y1] = true;
q[grid[x1][y1]].emplace(x1, y1);
}
}
}
}
}
cout << n;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |