이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
using namespace std;
typedef long long ll;
ll n, m;
ll check(ll x, ll y, vector<vector<ll>> &a, ll ok){
ll res = 0;
if(ok == 1){
res += ((x > 0 && y > 0 && x <= n && y <= m) ? a[x][y] : -1e9);
res += ((x - 1 > 0 && y > 0 && x - 1 <= n && y <= m) ? a[x - 1][y] : -1e9);
res += ((x > 0 && y - 1 > 0 && x <= n && y - 1 <= m) ? a[x][y - 1] : -1e9);
res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m) ? a[x][y + 1] : -1e9);
}
else if(ok == 2){
res += ((x > 0 && y > 0 && x <= n && y <= m) ? a[x][y] : -1e9);
res += ((x - 1 > 0 && y > 0 && x - 1 <= n && y <= m) ? a[x - 1][y] : -1e9);
res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m) ? a[x + 1][y] : -1e9);
res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m) ? a[x][y + 1] : -1e9);
}
else if(ok == 3){
res += ((x > 0 && y > 0 && x <= n && y <= m) ? a[x][y] : -1e9);
res += ((x > 0 && y - 1 > 0 && x <= n && y - 1 <= m) ? a[x][y - 1] : -1e9);
res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m) ? a[x + 1][y] : -1e9);
res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m) ? a[x][y + 1] : -1e9);
}
else if(ok == 4){
res += ((x > 0 && y > 0 && x <= n && y <= m) ? a[x][y] : -1e9);
res += ((x - 1 > 0 && y > 0 && x - 1 <= n && y <= m) ? a[x - 1][y] : -1e9);
res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m) ? a[x + 1][y] : -1e9);
res += ((x > 0 && y - 1 > 0 && x <= n && y - 1 <= m) ? a[x][y - 1] : -1e9);
}
return (res < 0 ? -1e9 : res);
}
int main(){
cin >> n >> m;
vector<vector<ll>>a(n + 1, vector<ll>(m + 1));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> a[i][j];
ll k;
cin >> k;
vector<pair<ll, ll>> d(k);
for(int i = 0; i < k; i++)
cin >> d[i].first >> d[i].second;
bool ok = false;
for(int i = 0; i < k; i++)
for(int j = i + 1; j < k; j++)
if(abs(d[i].first - d[j].first) <= 2 || abs(d[i].second - d[j].second) <= 2)
ok = true;
if(!ok){
ll ans = 0;
for(auto i: d){
ll cnt = max({check(i.first, i.second, a, 1),
check(i.first, i.second, a, 2),
check(i.first, i.second, a, 3),
check(i.first, i.second, a, 4)});
if(cnt == -1e9){
cout << -1 << endl;
return 0;
}
ans += cnt;
}
cout << ans << endl;
}
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... |