#include <bits/stdc++.h>
using namespace std;
#define el '\n'
#define ll long long
#define ld long double
#define ull unsigned long long
#define pll pair<long long, long long>
#define ppll pair< pair<long long, long long>, long long >
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
const ll DIM = 2e5 + 7;
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const ll maxlog = 20;
ll n, m, ans;
vector< vector<int> > a;
vector< vector<bool> > vis;
int x[17], y[17];
const vector<vector<pll>> d = {
{{0, 0}, {0, -1}, {0, 1}, {1, 0}},
{{0, 0}, {0, -1}, {0, 1}, {-1, 0}},
{{0, 0}, {-1, 0}, {1, 0}, {0, 1}},
{{0, 0}, {-1, 0}, {1, 0}, {0, -1}}
};
ll check(ll x, ll y, ll type) {
ll res = 0;
for (auto [dx, dy] : d[type]) {
ll nx = x+dx, ny = y+dy;
if (nx < 0 || nx >= n || ny < 0 || ny >= m || vis[nx][ny]) return -1;
vis[nx][ny] = 1;
res += a[nx][ny];
}
return res;
}
void solve() {
cin >> n >> m;
a.resize(n, vector<int>(m));
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cin >> a[i][j];
}
}
int k;
cin >> k;
for (int i=1; i<=k; i++) {
cin >> x[i] >> y[i];
}
ll answer = -INF;
vis.resize(n, vector<bool>(m, 0));
for (int mask = 0; mask < (1 << (2*k)); mask++) {
ll rmask = mask;
ans = 0;
int ex = 1;
vis.assign(n, vector<bool>(m, false));
for (int i=1; i<=k; i++) {
ll type = (rmask%2)*2 + (rmask/2) % 2;
rmask >>= 2;
ll cur = check(x[i], y[i], type);
if (cur == -1) {
ex = 0;
break;
}
ans += cur;
}
if (ex) answer = max(answer, ans);
}
if (answer == -INF) cout << "No" << el;
else cout << answer << el;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
//freopen("nocross.in", "r", stdin);
//freopen("nocross.out", "w", stdout);
int ntest = 1;
//cin >> ntest;
while (ntest--){
solve();
}
return 0;
}
;
# | 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... |