Submission #877887

#TimeUsernameProblemLanguageResultExecution timeMemory
877887konberT-Covering (eJOI19_covering)C++14
30 / 100
60 ms12124 KiB
#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)

covering.cpp: In function 'int main()':
covering.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     scanf("%d%d", &M, &N);
      |     ~~~~~^~~~~~~~~~~~~~~~
covering.cpp:56:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |             scanf("%d", &a[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~
covering.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     scanf("%d", &K);
      |     ~~~~~^~~~~~~~~~
covering.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%d%d", &sp[i].first, &sp[i].second);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...