1629-cake Slicing (DP)

Source: Internet
Author: User

It took nearly 2 hours to finally AC, good cool.

A similar to the problem of the optimal matrix chain, by the "cut stick" that question of revelation, the principle of the problem is the same, just become and area. The corresponding dimension is added as well.

It is obvious to have a complete representation of the state, with a minimum of four-dimensional arrays.   Represents the coordinates of its two diagonal vertices, respectively. Then cross-cut or lengthwise, and recursive to find a smaller rectangle, until only one cherry left in the rectangle returns 0

So the question is how to infer how many cherries are in a rectangle at a high speed, and then decide to open an array to record the number of cherries in the rectangle. It started out in this place (with a five-cycle), and then came up with a compromise that would spread the complexity of time.

By the efficient chapter, we can first use a two-dimensional array to find the number of cherries in each interval of each row, thus reducing the complexity of the time greatly.

Thought it would be very fast, the results seem to be unpleasant, the other people in the recursion when the number of cherry is actually too.

。 Perhaps it's because a memory search recursion will cut out a lot of unnecessary calculations.

See the code for details:

#include <bits/stdc++.h>using namespace Std;const int INF = 1000000;int N,M,K,MAXN = 0,d[22][22][22][22],cnt[22][ 22][22][22],f[22][22];struct point{int x, y;}    C[405];int dp (int ux,int uy,int dx,int dy) {int& ans = d[ux][uy][dx][dy];    if (cnt[ux][uy][dx][dy] = = 1) return 0;//recursive boundary if (ans >= 0) return ans;    ans = INF;        if (uy! = dy) for (int i=uy;i<=dy;i++) {//Slitting if (cnt[ux][uy][dx][i]>0 && cnt[ux][i][dx][dy]>0)    ans = min (ans,dp (ux,uy,dx,i) +DP (ux,i+1,dx,dy) + Dx-ux + 1);         } if (UX! = dx) for (int i=ux;i<=dx;i++) {//crosscutting if (cnt[ux][uy][i][dy]>0 && cnt[i][uy][dx][dy]>0)    ans = min (ANS,DP (i+1,uy,dx,dy) + DP (UX,UY,I,DY) + Dy-uy + 1); } return ans; void Init () {for (int. i=1;i<=k;i++) {for (int. r=1;r<=n;r++) {for (int j=1;j<=m;j++) {/////            The number of rows of cherries if (c[i].x = = R && c[i].y <= j) f[r][j]++; }}} for (int ux=1;ux<=n;ux++{for (int. uy=1;uy<=m;uy++) {for (int dx=ux;dx<=n;dx++) {for (int dy=uy;dy<=m;d                    y++) {int v = 0; for (int i=ux;i<=dx;i++) {v + = F[i][dy]-f[i][uy-1];//Find a part of the answer separately.                Dispersion of Time complexity} Cnt[ux][uy][dx][dy] = V;        }}}}}int main () {while (~scanf ("%d%d%d", &n,&m,&k)) {memset (f,0,sizeof (f));        Memset (d,-1,sizeof (d));        memset (cnt,0,sizeof (CNT));        for (int i=1;i<=k;i++) scanf ("%d%d", &c[i].x,&c[i].y);        Init ();    printf ("Case%d:%d\n", ++MAXN,DP (1,1,n,m)); } return 0;}



1629-cake Slicing (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.