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 long long ll;
typedef pair<int, int> ii;
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int h, w; cin >> h >> w;
string g[h];
for (int i = 0; i < h; ++i) {
cin >> g[i];
}
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
bool vis[h][w]; memset(vis, 0, sizeof vis);
int ans = 0;
queue<ii> q[2];
q[0].push(ii(0, 0));
vis[0][0] = 1;
while ((!q[0].empty()) || (!q[1].empty())) {
for (int x = 0; x < 2; ++x) {
if (!q[x].empty()) ++ans;
while (!q[x].empty()) {
ii u = q[x].front(); q[x].pop();
for (int i = 0; i < 4; ++i) {
ii v = ii(u.fi + dy[i], u.sc + dx[i]);
if (v.fi >= 0 && v.fi < h && v.sc >= 0 && v.sc < w && g[v.fi][v.sc] != '.' && !vis[v.fi][v.sc]) {
if (g[v.fi][v.sc] == g[u.fi][u.sc]) {
q[x].push(v);
}
else {
q[!x].push(v);
}
vis[v.fi][v.sc] = 1;
}
}
}
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |