Checkerboard Segmentation (Memory Search)

Source: Internet
Author: User

Checkerboard Segmentation

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 501 Accepted s Ubmission (s): 248

Problem description A 8*8 board with the following segmentation: Cut the original checkerboard down a rectangular checkerboard and make the rest of the rectangle, and then continue to split the remainder so that it has been cut (n-1) times, together with the last remaining rectangular checkerboard with a total of N-block rectangular chessboard. (Each cut can only be performed along the edge of the checkerboard lattice)  each lattice on the original board has a score, and the total score of a rectangular checkerboard is the sum of the scores of the squares it contains. Now it is necessary to divide the chessboard into N-block rectangular chessboard according to the above rules, and to minimize the average variance of the total score of each rectangular checkerboard. Mean variance, where the average XI is the total score of the block I rectangular checkerboard. Please program the given checkerboard and N to find the minimum value of O '.  

The input 1th behaves as an integer n (1 < n < 15).Lines 2nd through 9th each Act 8 non-negative integers less than 100, indicating the score of the corresponding lattice on the board. Each row is separated by a space between two adjacent numbers.

Output is only one number, which is O ' (rounded to three digits after the decimal point).

Sample Input31 1 1 1 1 1 1 31 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 01 1 1 1 1 1 0 3

Sample Output1.633

Exercises

Re a half-day, know the truth of my tears flow down, TMD, the number of steps is 15, I opened is 10;

Memory Search, Dp[x1][y1][x2][y2] represents the smallest s[i in this rectangular state] squared sum;

Here is a detailed explanation of the great God:

N Knife Cutting Board

The following is the 8*8 chess board, each number represents the weight of the chessboard corresponding point, asked to cut the N knife, each block and the average variance of the minimum is how much

The formula for mean variance requires simplification:

From the above, the minimum mean variance is obviously to xi^2 minimum

D[k][x1][y1][x2][y2] represents the smallest sum of squares obtained by a chessboard from (x1,y1) (X2,Y2) has been cut by the K-Knife

Use Sum[i][j] to represent weights and values from (from) points to (I,J) points

So the answer is dp[n][1][1][8][8]/n-(sum[8][8]/n) ^2

Use s[(x1,y1), (X2,y2)] to represent the weights between the two points and

Here, with a recursive DP

State transition equation:

d[k][x1][y1][x2][y2]=

min{ transverse cut: d[k-1] + the sum of the remaining unfinished portions of the square, longitudinal cut: d[k-1]+ The remainder of the square and}

The optimal solution for transverse cutting is min{d[k-1, (x1,y1), (i, y2)] + s[(i +1,y1), (x2, y2)], d[k-1, (i+1,y1), (x2, y2)] + s[(x1,y1), ( I, y2)]} (x1<= i <x2)

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>using namespacestd;Const intinf=0x3fffffff;#defineMem (x, y) memset (x,y,sizeof (x))#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)#defineSD (x) scanf ("%lf", &x)#defineP_ printf ("")typedefLong LongLL;Const intmaxn=Ten;Doubledp[ -][MAXN][MAXN][MAXN][MAXN];//Horizontal SlotintS[MAXN][MAXN];DoubleSS[MAXN][MAXN][MAXN][MAXN];DoubleSUM (intX1,intY1,intX2,inty2) {    if(ss[x1][y1][x2][y2]>=0)returnSs[x1][y1][x2][y2]; intTemp=s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1]; returnss[x1][y1][x2][y2]=temp*temp;}DoubleDfsintKintX1,intY1,intX2,inty2) {    if(k==1) {Dp[k][x1][y1][x2][y2]=SUM (X1,Y1,X2,Y2); returnDp[k][x1][y1][x2][y2]; }    if(dp[k][x1][y1][x2][y2]>=0)returnDp[k][x1][y1][x2][y2]; Doubletemp =99999999999;  for(inti=x1;i<x2;i++) {Temp=min (Temp,sum (X1,y1,i,y2) +dfs (K-1, i+1, Y1,x2,y2)); Temp=min (Temp,sum (i+1, Y1,x2,y2) +dfs (K-1, X1,y1,i,y2)); }     for(inti=y1;i<y2;i++) {Temp=min (Temp,sum (x1,i+1, X2,y2) +dfs (K-1, x1,y1,x2,i)); Temp=min (Temp,sum (x1,y1,x2,i) +dfs (K-1, x1,i+1, X2,y2)); }    returndp[k][x1][y1][x2][y2]=temp;}intMain () {intN;  while(~SI (N)) {        inttemp; MEM (SS,-1); Mem (S,0); Mem (dp,-1);  for(intI=1; i<=8; i++){             for(intj=1; j<=8; j + +) {scanf ("%d",&temp); S[I][J]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+temp; }        }        Doubleave=s[8][8]*1.0/N; DoubleAns=dfs (N,1,1,8,8); //printf ("%lf%d%lf\n", ans,s[8][8],ave);printf"%.3lf\n", sqrt (ans*1.0/n-ave*Ave)); }    return 0;}

Checkerboard Segmentation (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.