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 <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> pos;
vector<pair<int,int>> dir = {{1,0},{0,-1},{-1,0},{0,1}};
vector<vector<bool>> notin;
vector<vector<int>> grid;
int n,m;
int solve(int idx, int crt_ans){
if (idx == pos.size()) return crt_ans;
int ans = -1, A = pos[idx].first, B = pos[idx].second;
if (notin[A][B]) return 0;
notin[A][B] = true;
for (int j=0; j<4; ++j){
int a = crt_ans+grid[A][B];
for (int k=0; k<3; ++k){
int AA = A+dir[(j+k)%4].first, BB = B+dir[(j+k)%4].second;
if (AA >= n or BB >= m or AA < 0 or BB < 0 or notin[AA][BB]){
a = -1;
break;
}
a += grid[AA][BB];
}
if (a == -1) continue;
for (int i=0; i<3; ++i){
notin[A+dir[(j+i)%4].first][B+dir[(j+i)%4].second] = true;
}
ans = max(ans,solve(idx+1,a));
for (int i=0; i<3; ++i){
notin[A+dir[(j+i)%4].first][B+dir[(j+i)%4].second] = false;
}
}
notin[A][B] = false;
return ans;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
grid = vector<vector<int>>(n,vector<int>(m));
notin = vector<vector<bool>>(n,vector<bool>(m));
for (int i=0; i<n; ++i){
for (int j=0; j<m; ++j){
cin >> grid[i][j];
}
}
int q;
cin >> q;
for (int i=0; i<q; ++i){
int x,y;
cin >> x >> y;
pos.emplace_back(x,y);
}
int ans = solve(0,0);
if (ans == -1) cout << "No" << '\n';
else cout << ans << '\n';
return 0;
}
Compilation message (stderr)
covering.cpp: In function 'int solve(int, int)':
covering.cpp:11:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (idx == pos.size()) return crt_ans;
~~~~^~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |