POJ 1191 Chessboard Segmentation (Memory Search) __ Dynamic programming

Source: Internet
Author: User
Description

A 8*8 board is divided as follows: The original chessboard is cut off a rectangular chessboard and the remaining part is also rectangular, and then the remaining parts continue to be so divided, so cut (n-1) times, together with the last remaining rectangular board has a total of n rectangular chessboard. (Each cut can only be done along the side of the checkerboard grid)

Each square on the original board has a score, and the total score of a rectangular chessboard is the sum of the points it contains. Now it is necessary to divide the chessboard into N rectangular chessboard according to the above rules and minimize the mean variance of the total points of each rectangular board.

Mean Variance Σ=∑ni=1 (Xi−x¯) 2n−−−−−−−−−√σ=\sqrt{\frac{\sum^n_{i=1}{(x_i-\overline{x}) ^2}}{n}}, with an average value of x¯=∑ni=1xin \overline{x }=\frac{\sum^n_{i=1}{x_i}}{n}, Xi is the total score of block I rectangular chessboard.

Please program the given board and N to find the minimum value of O '.


Input

The 1th behavior is an integer n (1 < n < 15).

Lines 2nd through 9th are a nonnegative integer of 8 less than 100 for each action, representing the corresponding lattice score on the chessboard. Each row is separated by a space between the two numbers.


Output

Only one number, for O ' (rounded to three digits after the decimal point).


Sample Input

3-
1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1-----the----------+ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1--1------
C6/>1 1 1 1 1 1 1 0 1 1 1 1 1 1 0-
3


Sample Output

1.633


train of Thought

First, the opposite side of the formula for simplification, after simplification for σ2=∑ni=1x2in−x¯2σ^2=\frac{\sum^n_{i=1}{x_i^2}}{n}-\overline{x}^2

Then a DFS search is used to cut, dp[x][y][a][b][k] represents the N-k (x,y)-(a,b) score and squared after the cut.

Because the average is a fixed value, to minimize the variance, you can only make each block and as small as possible.


AC Code

#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <
Math.h> #include <iostream> using namespace std;
#include <queue> #include <map> #define INF (1<<25) int n,sum[10][10];

int dp[10][10][10][10][16];

int get (int x,int y,int A,int b) {return sum[a][b]-sum[a][y-1]-sum[x-1][b]+sum[x-1][y-1];}  int dfs (int x,int y,int a,int b,int k) {if (dp[x][y][a][b][k]!=-1) return dp[x][y][a][b][k];   Memory Search if (k==1) return (Dp[x][y][a][b][k]=get (x,y,a,b) *get (x,y,a,b));
    Returns the square int minn=inf of the sum of the current block;
        for (int i=x; i<a; i++)//Horizontal Cut {int l=get (X,Y,I,B);
        int R=get (I+1,Y,A,B);
    Minn=min (Minn,min (DFS (x,y,i,b,k-1) +r*r,dfs (i+1,y,a,b,k-1) +l*l));
        for (int i=y; i<b; i++)//Vertical Cut {int l=get (x,y,a,i);
        int R=get (X,I+1,A,B);
    Minn=min (Minn,min (DFS (x,y,a,i,k-1) +r*r,dfs (x,i+1,a,b,k-1) +l*l));
return (Dp[x][y][a][b][k]=minn); int main ()
{while (~scanf ("%d", &n)) {int temp;
        memset (sum,0,sizeof (sum));
        Memset (Dp,-1,sizeof (DP));
                for (int i=1; i<=8; i++) for (int j=1; j<=8; J + +) {scanf ("%d", &temp);   Sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+temp;
        From (1,1) to (i,j) area and} double avi=sum[8][8]*1.0/n;
    printf ("%.3f\n", sqrt (Dfs (1,1,8,8,n) *1.0/n-avi*avi));
return 0; }

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.