Submission #282979

#TimeUsernameProblemLanguageResultExecution timeMemory
282979lani1akeaT-Covering (eJOI19_covering)C++17
15 / 100
1089 ms2172 KiB
#include <bits/stdc++.h> #define F first #define S second #define ll long long #define pb push_back #define endl '\n' using namespace std; const int MOD = 1e9+7; const int N = 1e3; int n, m, k, g[N][N], x[N * 1000], y[N * 1000], ans = -1; void dfs(int nu, int used[N][N], int p) { if (nu == k) { ans = max(ans, p); return; } if (used[x[nu]][y[nu]]) { return; } if (x[nu] < n - 1 and y[nu] < m - 1 and y[nu] and !used[x[nu]][y[nu]] and !used[x[nu]][y[nu] - 1] and !used[x[nu]][y[nu] + 1] and !used[x[nu] + 1][y[nu]]) { // down used[x[nu]][y[nu]] = used[x[nu]][y[nu] - 1] = used[x[nu]][y[nu] + 1] = used[x[nu] + 1][y[nu]] = 1; p += g[x[nu]][y[nu]]; p += g[x[nu]][y[nu] - 1]; p += g[x[nu]][y[nu] + 1]; p += g[x[nu] + 1][y[nu]]; dfs(nu + 1, used, p); used[x[nu]][y[nu]] = used[x[nu]][y[nu] - 1] = used[x[nu]][y[nu] + 1] = used[x[nu] + 1][y[nu]] = 0; p -= g[x[nu]][y[nu]]; p -= g[x[nu]][y[nu] - 1]; p -= g[x[nu]][y[nu] + 1]; p -= g[x[nu] + 1][y[nu]]; } if (x[nu] and y[nu] < m - 1 and y[nu] and !used[x[nu]][y[nu]] and !used[x[nu]][y[nu] - 1] and !used[x[nu]][y[nu] + 1] and !used[x[nu] - 1][y[nu]]) { // up used[x[nu]][y[nu]] = used[x[nu]][y[nu] - 1] = used[x[nu]][y[nu] + 1] = used[x[nu] - 1][y[nu]] = 1; p += g[x[nu]][y[nu]]; p += g[x[nu]][y[nu] - 1]; p += g[x[nu]][y[nu] + 1]; p += g[x[nu] - 1][y[nu]]; dfs(nu + 1, used, p); used[x[nu]][y[nu]] = used[x[nu]][y[nu] - 1] = used[x[nu]][y[nu] + 1] = used[x[nu] - 1][y[nu]] = 0; p -= g[x[nu]][y[nu]]; p -= g[x[nu]][y[nu] - 1]; p -= g[x[nu]][y[nu] + 1]; p -= g[x[nu] - 1][y[nu]]; } if (x[nu] < n - 1 and x[nu] and y[nu] and !used[x[nu]][y[nu]] and !used[x[nu]][y[nu] - 1] and !used[x[nu] + 1][y[nu]] and !used[x[nu] - 1][y[nu]]) { // left used[x[nu]][y[nu]] = used[x[nu]][y[nu] - 1] = used[x[nu] + 1][y[nu]] = used[x[nu] - 1][y[nu]] = 1; p += g[x[nu]][y[nu]]; p += g[x[nu]][y[nu] - 1]; p += g[x[nu] + 1][y[nu]]; p += g[x[nu] - 1][y[nu]]; dfs(nu + 1, used, p); used[x[nu]][y[nu]] = used[x[nu]][y[nu] - 1] = used[x[nu] + 1][y[nu]] = used[x[nu] - 1][y[nu]] = 0; p -= g[x[nu]][y[nu]]; p -= g[x[nu]][y[nu] - 1]; p -= g[x[nu] + 1][y[nu]]; p -= g[x[nu] - 1][y[nu]]; } if (x[nu] < n - 1 and x[nu] and y[nu] < m - 1 and !used[x[nu]][y[nu]] and !used[x[nu]][y[nu] + 1] and !used[x[nu] + 1][y[nu]] and !used[x[nu] - 1][y[nu]]) { // right used[x[nu]][y[nu]] = used[x[nu]][y[nu] + 1] = used[x[nu] + 1][y[nu]] = used[x[nu] - 1][y[nu]] = 1; p += g[x[nu]][y[nu]]; p += g[x[nu]][y[nu] + 1]; p += g[x[nu] + 1][y[nu]]; p += g[x[nu] - 1][y[nu]]; dfs(nu + 1, used, p); used[x[nu]][y[nu]] = used[x[nu]][y[nu] + 1] = used[x[nu] + 1][y[nu]] = used[x[nu] - 1][y[nu]] = 0; p -= g[x[nu]][y[nu]]; p -= g[x[nu]][y[nu] + 1]; p -= g[x[nu] + 1][y[nu]]; p -= g[x[nu] - 1][y[nu]]; } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> g[i][j]; } } cin >> k; for (int i = 0; i < k; ++i) { cin >> x[i] >> y[i]; } int used[N][N]; dfs(0,used,0); if (ans == -1) cout << "No\n"; else cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...