# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
877887 | konber | T-Covering (eJOI19_covering) | C++14 | 60 ms | 12124 KiB |
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>
using namespace std;
vector<vector<int>> a, grid;
vector<pair<int, int>> sp;
int K, N, M;
int f(int x){
if(x==K) return 0;
int i=sp[x].first, j=sp[x].second;
int s1=-1, s2=-1, s3=-1, s4=-1;
if(i-1>=0 && i+1<M && j-1>=0){
if(grid[i][j]+grid[i-1][j]+grid[i+1][j]+grid[i][j-1]==0){
grid[i][j]=grid[i-1][j]=grid[i+1][j]=grid[i][j-1]=1;
int d=f(x+1);
if(d!=-1) s1 = a[i][j]+a[i-1][j]+a[i+1][j]+a[i][j-1]+d;
grid[i][j]=grid[i-1][j]=grid[i+1][j]=grid[i][j-1]=0;
}
}
if(i-1>=0 && i+1<M && j+1<N){
if(grid[i][j]+grid[i-1][j]+grid[i+1][j]+grid[i][j+1]==0){
grid[i][j]=grid[i-1][j]=grid[i+1][j]=grid[i][j+1]=1;
int d=f(x+1);
if(d!=-1) s2 = a[i][j]+a[i-1][j]+a[i+1][j]+a[i][j+1]+d;
grid[i][j]=grid[i-1][j]=grid[i+1][j]=grid[i][j+1]=0;
}
}
if(j-1>=0 && j+1<N && i+1<M){
if(grid[i][j]+grid[i][j-1]+grid[i][j+1]+grid[i+1][j]==0){
grid[i][j]=grid[i][j-1]=grid[i][j+1]=grid[i+1][j]=1;
int d=f(x+1);
if(d!=-1) s3 = a[i][j]+a[i][j-1]+a[i][j+1]+a[i+1][j]+d;
grid[i][j]=grid[i][j-1]=grid[i][j+1]=grid[i+1][j]=0;
}
}
if(j-1>=0 && j+1<N && i-1>=0){
if(grid[i][j]+grid[i][j-1]+grid[i][j+1]+grid[i-1][j]==0){
grid[i][j]=grid[i][j-1]=grid[i][j+1]=grid[i-1][j]=1;
int d=f(x+1);
if(d!=-1) s4 = a[i][j]+a[i][j-1]+a[i][j+1]+a[i-1][j]+d;
grid[i][j]=grid[i][j-1]=grid[i][j+1]=grid[i-1][j]=0;
}
}
return max(s1, max(s2, max(s3, s4)));
}
int main()
{
scanf("%d%d", &M, &N);
a.resize(M, vector<int>(N));
grid.resize(M, vector<int>(N));
for(int i=0; i < M; i++){
for(int j=0; j < N; j++)
scanf("%d", &a[i][j]);
}
scanf("%d", &K);
sp.resize(K);
for(int i=0; i < K; i++){
scanf("%d%d", &sp[i].first, &sp[i].second);
}
if(K <= 10){
int ans=f(0);
if(ans>=0) printf("%d\n", ans);
else printf("No\n");
}
else{
for(int i=0; i < K; i++){
grid[sp[i].first][sp[i].second] = 1;
}
int sum=0;
bool valid=true;
for(int x=0; x < K; x++){
int i=sp[x].first, j=sp[x].second;
int s1=-1, s2=-1, s3=-1, s4=-1;
if(i-1>=0 && i+1<M && j-1>=0 && grid[i-1][j]+grid[i+1][j]+grid[i][j-1]==0)
s1=a[i][j]+a[i-1][j]+a[i+1][j]+a[i][j-1];
if(i-1>=0 && i+1<M && j+1<N && grid[i-1][j]+grid[i+1][j]+grid[i][j+1]==0)
s2=a[i][j]+a[i-1][j]+a[i+1][j]+a[i][j+1];
if(j-1>=0 && j+1<N && i-1>=0 && grid[i][j-1]+grid[i][j+1]+grid[i-1][j]==0)
s3=a[i][j]+a[i][j-1]+a[i][j+1]+a[i-1][j];
if(j-1>=0 && j+1<N && i+1<M && grid[i][j-1]+grid[i][j+1]+grid[i+1][j]==0)
s4=a[i][j]+a[i][j-1]+a[i][j+1]+a[i+1][j];
int d=max(s1, max(s2, max(s3, s4)));
if(d==-1){
valid=false;
break;
}
sum += d;
}
if(!valid) printf("No\n");
else printf("%d\n", sum);
}
}
Compilation message (stderr)
# | 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... |