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 <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 (stderr)
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())
| ~~~~~~~^~~~~~~~~~~~~~~
# | 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... |