POJ 1191 Checkerboard Segmentation (compression dp+ memory Search)

Source: Internet
Author: User

One, test instructions:

Chinese questions

Second, analysis:

The main use of compressed DP and memory search ideas

Three, code:

#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include < math.h>using namespace Std;const int big=20000000;int mat[10][10];int n;int sum[10][10];int dp[20][10][10][10][10];    The Void unit () {//sum with SUM[I][J] represents the IJ to the upper-left corner and int total;            for (int i=1, i<=8; i++) for (int j=1; j<=8; J + +) {Total=mat[i][j];            int x_1,y_1;            X_1=i-1;            Y_1=j-1;            if (x_1>0) total+=sum[x_1][j];            if (y_1>0) total+=sum[i][y_1];            if (x_1>0&&y_1>0) total-=sum[x_1][y_1];        Sum[i][j]=total;    }}int account (int x_1,int y_1,int x_2,int y_2) {//Request (X_1,y_1) to (x_2,y_2) interval and int total=sum[x_2][y_2];    int x_3,y_3;    X_3=x_1-1;    Y_3=y_1-1;    if (x_3>0) total-=sum[x_3][y_2];    if (y_3>0) total-=sum[x_2][y_3];    if (x_3>0&&y_3>0) total+=sum[x_3][y_3]; return total*total;} INT Solve (int k,int x_1,int y_1,int x_2,int y_2) {//Memory DP if (DP[K][X_1][Y_1][X_2][Y_2]!=-1) return dp[k][x_1][y_1] [X_2]    [Y_2];    if (k==1) return Dp[k][x_1][y_1][x_2][y_2]=account (x_1,y_1,x_2,y_2);        if (k>1) {int min=big;            for (int i=y_1;i<y_2;i++) {//crosswise cut int first=account (x_1,y_1,x_2,i);            int Second=account (x_1,i+1,x_2,y_2);            First+=solve (k-1,x_1,i+1,x_2,y_2);            Second+=solve (K-1,x_1,y_1,x_2,i);            if (First>second) First=second;        if (Min>first) Min=first;            } for (int j=x_1;j<x_2;j++) {//lengthwise cut int first=account (x_1,y_1,j,y_2);            int Second=account (j+1,y_1,x_2,y_2);            First+=solve (k-1,j+1,y_1,x_2,y_2);            Second+=solve (k-1,x_1,y_1,j,y_2);            if (First>second) First=second;        if (Min>first) Min=first; } return dp[k][x_1][y_1][x_2][Y_2]=min; } return dp[k][x_1][y_1][x_2][y_2]=big;}        int main () {while (scanf ("%d", &n)!=eof) {double total=0.0;                 for (int. I=1; i<=8; i++) for (int j=1; j<=8; J + +) {scanf ("%d", &mat[i][j]);            TOTAL+=MAT[I][J];        } unit ();         Memset (Dp,-1,sizeof (DP));        Total= (total/n) * (total/n);        Double Key=solve (n,1,1,8,8);       key=key/n;       KEY=SQRT (key-total);    printf ("%0.3f\n", key); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1191 Checkerboard Segmentation (compression dp+ memory Search)

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.