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 N (int)20
int cnt, n, m, used[N][N], deg[N][N];
char a[N][N], b[N][N];
void dfs(int i, int j) {
used[i][j] = cnt;
if(b[i][j] == '0') {
int ni = i, nj = j - 1;
if(ni >= 0 && ni < n && nj >= 0 && nj < m) dfs(ni, nj);
}
if(b[i][j+1] == '0') {
int ni = i, nj = j + 1;
if(ni >= 0 && ni < n && nj >= 0 && nj < m) dfs(ni, nj);
}
if(a[i][j] == '0') {
int ni = i-1, nj = j;
if(ni >= 0 && ni < n && nj >= 0 && nj < m) dfs(ni, nj);
}
if(a[i+1][j] == '0') {
int ni = i+1, nj = j;
if(ni >= 0 && ni < n && nj >= 0 && nj < m) dfs(ni, nj);
}
}
signed main () {
cnt = 1;
scanf("%d%d", &n, &m);
for(int i = 0; i <= n; i ++) {
for(int j = 0; j < m; j ++) std::cin >> a[i][j];
}
for(int i = 0; i < n; i ++) {
for(int j = 0; j <= m; j ++) std::cin >> b[i][j];
}
for(int i = 0; i < n; i ++ ) {
for(int j = 0; j < m; j ++) {
deg[i][j] += (a[i][j] == '1') + (a[i+1][j] == '1') + (b[i][j] == '1') + (b[i][j + 1] == '1');
}
}
for(int i = 0; i < n; i ++) {
for(int j = 0; j < m; j ++) {
if(used[i][j] || deg[i][j] == 4) continue;
dfs(i, j);
cnt ++;
}
}
int cnt1 = 0, cnt2 = 0;
for(int i = 0; i < n; i ++) {
for(int j = 0; j < m; j ++) {
if(used[i][j] == 1) cnt1 ++;
else if(used[i][j] == 2) cnt2 ++;
}
}
if(std::min(cnt1, cnt2) == 0) printf("%d", -std::max(cnt1, cnt2));
else
printf("%d", std::max(cnt1, cnt2) - std::min(cnt1, cnt2));
return 0;
}
Compilation message (stderr)
game.cpp: In function 'int main()':
game.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |