1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| const int maxn = 15 + 1; const int MOD = 16; const int maxk = 225 + 1; int N, M, K; int A[maxn][maxn];
int f[maxn][maxk][maxn][maxn][2][2]; int pre[maxn][maxk][maxn][maxn][2][2];
int code(int l, int r, int dl, int dr) { return (l << 12) + (r << 8) + (dl << 4) + dr; }
void decode(const int val, int& l, int& r, int& dr, int& dl) { l = (val >> 12) % MOD; r = (val >> 8) % MOD; dr = (val >> 4) % MOD; dl = (val) % MOD; }
void initDP() { Set(f, 0); Set(pre, 0); }
int dp(int& ans, int& fi) { initDP();
_rep(i, 1, N) _rep(k, 1, K) _rep(l, 1, M) _rep(r, 1, M) { if(k < (r - l + 1)) continue;
// (spand, spand) int& _f1 = f[i][k][l][r][1][0]; int& _pre1 = pre[i][k][l][r][1][0];
int maxv1 = 0; _rep(p, l, r) _rep(q, p, r) { const int last = f[i - 1][k - (r - l + 1)][p][q][1][0]; if(maxv1 < last) { maxv1 = last; //_pre1 = {i - 1, k - (r - l + 1), p, q, 1, 0}; //debug(_pre1.k); //_pre1 = code(p, q, 1, 0); _pre1 = (p << 12) + (q << 8) + (1 << 4) + 0; } } _rep(j, l, r) maxv1 += A[i][j]; _f1 = maxv1;
// (spand, shrink) int& _f2 = f[i][k][l][r][1][1]; int& _pre2 = pre[i][k][l][r][1][1];
int maxv2 = 0; _rep(p, l, r) _rep(q, r, M) _for(dr, 0, 2) { const int last = f[i - 1][k - (r - l + 1)][p][q][1][dr]; if(maxv2 < last) { maxv2 = last; //_pre2 = code(p, q, 1, dr); //debug(_pre2.k); _pre2 = (p << 12) + (q << 8) + (1 << 4) + dr; } } _rep(j, l, r) maxv2 += A[i][j]; _f2 = maxv2;
// (shrink, spand) int& _f3 = f[i][k][l][r][0][0]; int& _pre3 = pre[i][k][l][r][0][0];
int maxv3 = 0; _rep(p, 1, l) _rep(q, l, r) _for(dl, 0, 2) { const int last = f[i - 1][k - (r - l + 1)][p][q][dl][0]; if(maxv3 < last) { maxv3 = last; //_pre3 = code(p, q, dl, 0); //debug(_pre3.k); _pre3 = (p << 12) + (q << 8) + (dl << 4) + 0; } } _rep(j, l, r) maxv3 += A[i][j]; _f3 = maxv3;
// (shrink, shrink) int& _f4 = f[i][k][l][r][0][1]; int& _pre4 = pre[i][k][l][r][0][1];
int maxv4 = 0; _rep(p, 1, l) _rep(q, r, M) _for(dl, 0, 2) _for(dr, 0, 2) { const int last = f[i - 1][k - (r - l + 1)][p][q][dl][dr]; if(maxv4 < last) { maxv4 = last; //_pre4 = code(p, q, dl, dr); //debug(_pre4.k); _pre4 = (p << 12) + (q << 8) + (dl << 4) + dr; } } _rep(j, l, r) maxv4 += A[i][j]; _f4 = maxv4; }
// then get ret; int ret = 0; _rep(i, 1, N) _rep(l, 1, M) _rep(r, l, M) _for(dl, 0, 2) _for(dr, 0, 2) { if(ret < f[i][K][l][r][dl][dr]) { ret = f[i][K][l][r][dl][dr]; //ans = {i, K, l, r, dl, dr}; // ans = code(l, r, dl, dr); ans = (l << 12) + (r << 8) + (dl << 4) + dr; fi = i; } }
return ret; }
void printAns(int ans, int row, int K) { if(row == 0 || K <= 0) return; int al, ar, adl, adr; //decode(ans, al, ar, adl, adr); al = (ans >> 12) % MOD; ar = (ans >> 8) % MOD; adl = (ans >> 4) % MOD; adr = (ans) % MOD;
_rep(i, al, ar) printf("%d %d\n", row, i);
printAns(pre[row][K][al][ar][adl][adr], row - 1, K - (ar - al + 1)); }
int main() { freopen("input.txt", "r", stdin); scanf("%d%d%d", &N, &M, &K);
_rep(i, 1, N) _rep(j, 1, M) scanf("%d", &A[i][j]);
// then dp
int ans, ai; printf("Oil : %d\n", dp(ans, ai));
printAns(ans, ai, K); }
|