#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
#define le first.first
#define ri first.second
#define dw second.first
#define up second.second
vector<vector<int>>gra;
vector<bool>visit;
vector<int>com;
void dfs(int v,int com1) {
visit[v] = true;
com[v] = com1;
for (int i = 0; i < gra[v].size(); i++) {
if (!visit[gra[v][i]])
dfs(gra[v][i],com1);
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>>hez((n + 1), vector<int>(m)), ver((n), vector<int>(m+1));
string a;
for (int i = 0; i <= n; i++) {
cin >> a;
for (int j = 0; j < m; j++) {
if (a[j] == '1')
hez[i][j] = 1;
else
hez[i][j] = 0;
}
}
for (int i = 0; i < n; i++) {
cin >> a;
for (int j = 0; j <= m; j++) {
if (a[j] == '1')
ver[i][j] = 1;
else
ver[i][j] = 0;
}
}
vector<pair<pii, pii>>vec(n*m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
vec[i * m + j].le = ver[i][j];
vec[i * m + j].ri = ver[i][j + 1];
vec[i * m + j].up = hez[i][j];
vec[i * m + j].dw = hez[i + 1][j];
}
}
int cnt = n * m;
/*
for (int i = 0; i < n * m; i++) {
if (vec[i].le == 1 and vec[i].ri == 1 and vec[i].up == 1 and vec[i].dw == 1)
cnt++;
}
cnt = n * m - cnt;*/
visit.resize(cnt, false);
gra.resize(cnt);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i != 0 and vec[i * m + j].up == 0)
gra[i * m + j].push_back((i - 1) * m + j);
if (i != n - 1 and vec[i * m + j].dw == 0)
gra[i * m + j].push_back((i + 1) * m + j);
if (j != 0 and vec[i * m + j].le == 0)
gra[i * m + j].push_back(i * m + j - 1);
if (j != m - 1 and vec[i * m + j].ri == 0)
gra[i * m + j].push_back(i * m + j + 1);
}
}
if (gra[0].size() == 0 and (vec[0].le != 1 or vec[0].ri != 1 or vec[0].up != 1 or vec[0].dw != 1))
gra[0].push_back(0);
if (gra[m - 1].size() == 0 and (vec[m - 1].le != 1 or vec[m - 1].ri != 1 or vec[m - 1].up != 1 or vec[m - 1].dw != 1))
gra[m - 1].push_back(m - 1);
if (gra[(n - 1) * m].size() == 0 and (vec[(n - 1) * m].le != 1 or vec[(n - 1) * m].ri != 1 or vec[(n - 1) * m].up != 1 or vec[(n - 1) * m].dw != 1))
gra[(n - 1) * m].push_back((n - 1) * m);
if (gra[n * m - 1].size() == 0 and (vec[n * m - 1].le != 1 or vec[n * m - 1].ri != 1 or vec[n * m - 1].up != 1 or vec[n * m - 1].dw != 1))
gra[n * m - 1].push_back(n * m - 1);
int tmp = 0;
com.resize(cnt, -1);
for (int i = 0; i < cnt; i++) {
if (!visit[i] and gra[i].size() != 0) {
tmp++;
dfs(i, tmp);
}
}
vector<int>rec(tmp, 0);
for (int i = 0; i < cnt; i++) {
if (com[i] != -1) {
rec[com[i] - 1]++;
}
}
sort(rec.begin(), rec.end());
if (rec.size() == 1)
cout << -rec[0];
else {
if (rec[0] > 2)
cout << rec[0] - rec[1];
else
cout << rec[1] - rec[0];
}
}
Compilation message
game.cpp: In function 'void dfs(int, int)':
game.cpp:15:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for (int i = 0; i < gra[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |