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"
#define endl '\n'
const int maxn = 1005;
const int inf = 2e9;
const int mod = 1e9 + 7;
using namespace std;
using lint = long long;
using pii = pair<int,int>;
int n, m;
int ans;
char g[maxn][maxn];
int t[maxn][maxn];
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
bool check(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && g[x][y] != '*';
}
void dfs(int x, int y) {
for(int a : dx)
for(int b : dy)
if(check(x + a, y + b) && g[x + a][y + b] == g[x][y] && !t[x + a][y + b]) {
t[x + a][y + b] = t[x][y];
dfs(x + a, y + b);
}
g[x][y] = g[x][y] == 'B' ? 'T' : 'B';
}
signed main() {
#ifdef Local
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> g[i];
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++) {
if(t[x][y] || g[x][y] == '*')
continue;
bool ok = false;
for(int a : dx)
for(int b : dy)
if(check(x + a, y + b) && t[x + a][y + b] && g[x + a][y + b] != g[x][y]) {
ok = true;
t[x][y] = t[x + a][y + b];
}
if(!ok) {
ans += !ok;
t[x][y] = ans;
}
dfs(x, y);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |