There is an n row m-column of mesh cakes, with some cherries on it, so that each cake has a cherry cut of the minimum length
Idea: DP.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = + 5;const int INF = 10000000;int d[22][22][22][22];int N, M, K, Kase = 0;//cherry number int cherry[22][22], Sumv[22][22];int CPU (int A, int b, int c, int d) {return sumv[b][d]-sumv[a-1][d]-sumv[b][c-1] + sumv[a-1][c-1];} int dp (int begx, int endx, int begy, int endy, int cherrynum) {int& ans = d[begx][endx][begy][endy];if (Cherrynum = = 1) Return 0;if (ans! =-1) return Ans;ans = inf;for (int i = BEGX; i < endx; i++) if (CPU (BEGX, I, Begy, Endy) > 0 && Amp CPU (BEGX, I, Begy, Endy) < cherrynum) ans = min (ans, DP (BEGX, I, Begy, Endy, CPU (BEGX, I, Begy, Endy)) +DP (i+1, EndX, Beg Y, Endy, cherrynum-cpu (BEGX, I, Begy, Endy)) +endy-begy+1); for (int i = begy; i < Endy; i++) if (CPU (BEGX, EndX, Begy, i) > 0 && CPU (begx, en DX, begy, i) < cherrynum) ans = min (ans, DP (BEGX, ENDX, Begy, I, CPU (BEGX, EndX, Begy, i)) +DP (BEGX, EndX, i+1, Endy, che Rrynum-cpu (BEGX, EndX, Begy, i)) +endx-begx+1); return ans; }void init () {memset (d,-1, sizeof (d)), memset (cherry, 0, sizeof (cherry)), memset (SUMV, 0, sizeof (SUMV)), int x, y;for (int i = 0; i < K; i++) {cin >> x >> y;cherry[x][y] = 1;} for (int i = 1, i <= N; i++) for (int j = 1; j <= M; j + +) Sumv[i][j] = Sumv[i][j-1] + sumv[i-1][j] + cherry[i][j]-sum V[I-1][J-1];} void Solve () {printf ("Case%d:%d\n", ++kase, DP (1, N, 1, M, K));} int main () {//freopen ("Input.txt", "R", stdin), while (scanf ("%d%d%d", &n, &m, &k) = = 3) {init (); Solve ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 1629 cut cake (DP)