#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int inf = 1e9 + 5, maxn = 21;
vector<int> cycles, paths;
map<vector<int>, int> m;
void chmin(int& a, const int& b) { a = min(a, b); }
void chmax(int& a, const int& b) { a = max(a, b); }
int solve(int ic, int ip, int left, int typ) // typ = -1 ak to je uz odseknute, 0 je cesta, 1 je cyklus
{
if (ic+1 == cycles.size() && ip+1 == paths.size()) return left;
if (m.count({ ic, ip, left, typ })) return m[{ic, ip, left, typ }];
int &ans = m[{ic, ip, left, typ }] = -inf;
if (typ == 0 && left > 2) chmax(ans, left - 2 - solve(ic, ip, 2, -1));
if (typ == 1 && left >= 4) chmax(ans, left - 4 - solve(ic, ip, 4, -1));
if (ic + 1 != cycles.size())
chmax(ans, left - solve(ic + 1, ip, cycles[ic + 1], 1));
if (ip + 1 != paths.size())
chmax(ans, left - solve(ic, ip + 1, paths[ip + 1], 0));
return ans;
}
vector<vector<int> > g(maxn * maxn);
vector<string> h(maxn), v(maxn);
vector<bool> vis(maxn * maxn, false);
int r, c;
void dfs(int u, int& cntv, int& cnte)
{
//cout << "(" << u / c << " " << u % c << ") ";
cntv++, cnte += g[u].size(), vis[u] = true;
for (int v : g[u]) if (!vis[v]) dfs(v, cntv, cnte);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> r >> c;
for (int i = 0; i < r + 1; i++) cin >> h[i];
for (int i = 0; i < r; i++) cin >> v[i];
for (int i = 0; i < r; i++) for (int j = 0; j < c; j++)
{
int num = i * c + j;
if (i && h[i][j] == '0') g[num].push_back(num - c);
if (i < r-1 && h[i+1][j] == '0') g[num].push_back(num + c);
if (j && v[i][j] == '0') g[num].push_back(num - 1);
if (j < c-1 && v[i][j+1] == '0') g[num].push_back(num + 1);
if (h[i][j] == '1' && h[i + 1][j] == '1' && v[i][j] == '1' && v[i][j + 1] == '1') vis[num] = true;
}
for (int i = 0; i < r; i++) for (int j = 0; j < c; j++)
{
int num = i * c + j;
if (vis[num]) continue;
int cntv = 0, cnte = 0;
//cout << "new: ";
dfs(num, cntv, cnte);
//cout << "\n";
if (cntv * 2 > cnte) paths.push_back(cntv);
else cycles.push_back(cntv);
}
sort(paths.begin(), paths.end());
sort(cycles.begin(), cycles.end());
cout << solve(-1, -1, 0, -1) << "\n";
return 0;
}
Compilation message
game.cpp: In function 'int solve(int, int, int, int)':
game.cpp:14:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if (ic+1 == cycles.size() && ip+1 == paths.size()) return left;
| ~~~~~^~~~~~~~~~~~~~~~
game.cpp:14:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if (ic+1 == cycles.size() && ip+1 == paths.size()) return left;
| ~~~~~^~~~~~~~~~~~~~~
game.cpp:19:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | if (ic + 1 != cycles.size())
| ~~~~~~~^~~~~~~~~~~~~~~~
game.cpp:21:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | if (ip + 1 != paths.size())
| ~~~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
1 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 |
1 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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
324 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
0 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
204 KB |
Output is correct |
55 |
Correct |
1 ms |
204 KB |
Output is correct |
56 |
Correct |
1 ms |
204 KB |
Output is correct |
57 |
Correct |
1 ms |
324 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
324 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
1 ms |
332 KB |
Output is correct |
66 |
Correct |
3 ms |
576 KB |
Output is correct |
67 |
Correct |
5 ms |
716 KB |
Output is correct |
68 |
Correct |
4 ms |
588 KB |
Output is correct |
69 |
Correct |
3 ms |
588 KB |
Output is correct |
70 |
Correct |
3 ms |
588 KB |
Output is correct |
71 |
Correct |
3 ms |
588 KB |
Output is correct |
72 |
Correct |
4 ms |
716 KB |
Output is correct |
73 |
Correct |
9 ms |
1348 KB |
Output is correct |
74 |
Correct |
3 ms |
588 KB |
Output is correct |
75 |
Correct |
3 ms |
588 KB |
Output is correct |
76 |
Correct |
3 ms |
588 KB |
Output is correct |
77 |
Correct |
4 ms |
716 KB |
Output is correct |
78 |
Correct |
3 ms |
716 KB |
Output is correct |
79 |
Correct |
3 ms |
588 KB |
Output is correct |
80 |
Correct |
4 ms |
716 KB |
Output is correct |
81 |
Correct |
3 ms |
588 KB |
Output is correct |
82 |
Correct |
4 ms |
716 KB |
Output is correct |
83 |
Correct |
3 ms |
716 KB |
Output is correct |