# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
272655 | mayhoubsaleh | Domino (COCI15_domino) | C++14 | 0 ms | 0 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 <bits/stdc++.h>
#define ll long long
#define pb push_back
#define left 2*i+1
#define righ 2*i+2
#define mid (l+r)/2
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn=2222;
const int inf=1e12+10;
const int mod=1e9+7;
const int base=27;
int n,k;
int dp[maxn][maxn][11];
int dpr[maxn][maxn][11];
int dpc[maxn][maxn][11];
int a[maxn][maxn];
int sum;
int main()
{
IOS
cin>>n>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
sum+=a[i][j];
}
}
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
for(int d=0;d<=k;d++){
dp[i][j][d]=-inf;
dpr[i][j][d]=-inf;
dpc[i][j][d]=-inf;
}
}
}
for(int r=1;r<=n;r++){
dpr[r][0][0]=0;
for(int c=1;c<=n;c++){
for(int d=0;d<=k;d++){
dpr[r][c][d]=dpr[r][c-1][d];
if(c-2>=0&&d.0)dpr[r][c][d]=max(dpr[r][c][d],a[r][c]+a[r][c-1]+dpr[r][c-2][d-1]);
}
}
}
/*for(int d=0;d<=k;d++){
cout<<"******"<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<dpr[i][j][d]<<' ';
}
cout<<endl;
}
}*/
for(int c=1;c<=n;c++){
dpc[0][c][0]=0;
for(int r=1;r<=n;r++){
for(int d=0;d<=k;d++){
dpc[r][c][d]=dpc[r-1][c][d];
if(r-2>=0&&d>0)dpc[r][c][d]=max(dpc[r][c][d],a[r][c]+a[r-1][c]+dpc[r-2][c][d-1]);
}
}
}
for(int i=0;i<=n;i++){
dp[0][i][0]=dp[i][0][0]=0;
}
for(int r=1;r<=n;r++){
for(int c=1;c<=n;c++){
for(int d=0;d<=k;d++){
for(int d1=0;d1<=d;d1++){
int d2=d-d1;
dp[r][c][d]=max(dp[r][c][d],dpr[r][c][d1]+dp[r-1][c][d2]);
dp[r][c][d]=max(dp[r][c][d],dpc[r][c][d1]+dp[r][c-1][d2]);
}
}
}
}
cout<<sum-dp[n][n][k]<<endl;
return 0;
}