#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
const unsigned int MAXN = (int)2e+1 + 5;
const unsigned int MAXMN = 410;
int grid[MAXN][MAXN];
int N, M;
int cnt;
int p[MAXMN];
int sz[MAXMN];
int CI(int x, int y) {return (y-1)*M + x;}
pair<int,int> IC(int id) {return {(id-1)%M +1, ((id-1)/M)+1};}
int findp(int u){
if(p[u] == u) return u;
return p[u] = findp(p[u]);
}
void Union(int u, int v){
if(findp(v) == findp(u)) return;
if(sz[findp(v)] > sz[findp(u)]) swap(u,v);
sz[findp(u)] += sz[findp(v)];
// int x = findp(u), y = findp(v);
// printf("%d\n", findp(v));
// p[2]= 1;
p[findp(v)] = findp(u);
}
void Join(int x, int y, int dx, int dy){
// printf("\n\n%d %d\n\n", x, y);
if(x < 1 || x > M || y < 0 || y > N) return;
if(dx < 1 || dx > M || dy < 0 || dy > N) return;
Union(CI(x,y), CI(dx,dy));
}
int main(){
scanf("%d%d", &N, &M);
for(int i = 1; i<= N*M; ++i){
p[i] = i;
sz[i] = 1;
}
char ps[M];
scanf("%s", ps);
for(int i = 1; i<=N; ++i){
char s[M];
scanf("%s", s);
for(int j = 1; j <=M;++j){
grid[i][j] = (ps[j-1]-'0') + (s[j-1]-'0');
if(s[j-1]-'0' == 0) Join(j, i, j, i+1);
}
for(int j = 0; j<M;++j){
ps[j] = s[j];
}
}
for(int i = 1; i<=N; ++i){
char s[M+1];
scanf("%s", s);
for(int j = 1; j<=M; ++j){
grid[i][j] += (s[j] -'0') + (s[j-1] - '0');
if(s[j-1] == '0') Join(j, i, j-1, i);
if(grid[i][j] == 4) cnt++;
}
}
int f = 0, s = 0;
bool onlytwo = true;
for(int i = 1; i<=M*N;++i){
if(grid[IC(i).Y][IC(i).X] == 4) continue;
if(f==0)f = findp(i);
else if(s==0 && f != findp(i)) s= findp(i);
else if(findp(i) != s && findp(i) != f) onlytwo = false;
}
if(onlytwo && s == 0){
printf("%d", -sz[f]);
}
else if(onlytwo){
if(sz[findp(f)] > 1 && sz[findp(s)] > 1) printf("%d", 2 - (sz[findp(f)]+sz[findp(s)]-2));
else if(sz[findp(f)] ==1 && sz[findp(s)] > 1) printf("%d", (sz[findp(s)]) - 1);
else if(sz[findp(s)] ==1 && sz[findp(f)] > 1) printf("%d", (sz[findp(f)]) - 1);
else printf("0");
}
else
// for(int i = 1; i<=N;++i){
// for(int j = 1; j <=M;++j){
// printf("%d ", grid[i][j]);
// }
// printf("\n");
// }
return 0;
}
Compilation message
game.cpp: In function 'int main()':
game.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
game.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%s", ps);
| ~~~~~^~~~~~~~~~
game.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%s", s);
| ~~~~~^~~~~~~~~
game.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%s", s);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |