DP,当然大家题解写得很详细我也没有什么好说的。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
const int LEN = 16, MAXK = LEN*LEN, Nya = -1, SIZE = (1<<4)-1;
typedef int Fun[LEN][LEN][LEN][MAXK][4];
typedef int Array[LEN][LEN];
int n, m, k;
Array map, sum;
Fun f, g;
int maxrow, maxl, maxr;
int maxk, type, maxf = Nya;
int GetSite(int l,int r,int t)
{
return (t << 8)|(l << 4)|(r);
}
int Dp(int row,int l,int r,int cost,int t)
{
int &ret = f[row][l][r][cost][t];
int &fr = g[row][l][r][cost][t];
int tmp;
ret = 0, cost -= r-l+1;
switch(t)
{
case 0:
for(int i = l; i <= r; i ++)
for(int j = i; j <= r; j++)
{
if(f[row-1][i][j][cost][0] > ret)
ret = f[row-1][i][j][cost][0], fr = GetSite(i,j,0);
}
break;
case 1:
for(int i = 1; i <= l; i ++)
for(int j = l; j <= r; j++)
{
if(f[row-1][i][j][cost][0] > ret)
ret = f[row-1][i][j][cost][0], fr = GetSite(i,j,0);
if(f[row-1][i][j][cost][1] > ret)
ret = f[row-1][i][j][cost][1], fr = GetSite(i,j,1);
}
break;
case 2:
for(int i = l; i <= r; i++)
for(int j = r; j <= m; j++)
{
if(f[row-1][i][j][cost][0] > ret)
ret = f[row-1][i][j][cost][0], fr = GetSite(i,j,0);
if(f[row-1][i][j][cost][2] > ret)
ret = f[row-1][i][j][cost][2], fr = GetSite(i,j,2);
}
break;
case 3:
for(int i = 1; i <= l; i ++)
for(int j = r; j <= m; j++)
{
if(f[row-1][i][j][cost][0] > ret)
ret = f[row-1][i][j][cost][0], fr = GetSite(i,j,0);
if(f[row-1][i][j][cost][1] > ret)
ret = f[row-1][i][j][cost][1], fr = GetSite(i,j,1);
if(f[row-1][i][j][cost][2] > ret)
ret = f[row-1][i][j][cost][2], fr = GetSite(i,j,2);
if(f[row-1][i][j][cost][3] > ret)
ret = f[row-1][i][j][cost][3], fr = GetSite(i,j,3);
}
break;
}
ret += sum[row][r] - sum[row][l-1];
return ret;
}
void prt(int row,int type,int l,int r,int cost)
{
if(!cost) return;
int y = g[row][l][r][cost][type];
prt(row-1,(y>>8),(y>>4)&SIZE,(y)&SIZE,cost-(r-l+1));
for(int i = l; i <= r; i++)
std::cout << row << ' ' << i << std::endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("sgu167.in","r",stdin);
freopen("sgu167.out","w",stdout);
#endif
std::cin >> n >> m >> k;
memset(f, Nya, sizeof(f));
memset(g, Nya, sizeof(g));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
std::cin >> map[i][j];
sum[i][j] = sum[i][j-1]+map[i][j];
}
if(k == 0)
std::cout << "Oil : 0" << std::endl;
else
{
for(int i = 1; i <= n; i++)
for(int l = 1; l <= m; l++)
for(int r = l; r <= m; r++)
for(int c = r-l+1; c <= k; c++)
for(int tp = 0; tp < 4; tp++)
{
int ff = Dp(i,l,r,c,tp);
if(c == k && ff > maxf)
{
maxrow = i, maxl = l,maxr = r;
maxk = c, type = tp, maxf = ff;
}
}
std::cout << "Oil : " << maxf << std::endl;
prt(maxrow, type, maxl, maxr, maxk);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。