Description
A 8*8 board is split as follows: The original board is cut down a rectangular checkerboard and the rest is also a rectangle, and then the remainder continues to be so segmented, so that after cutting (n-1), together with the last remaining rectangular checkerboard there is 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 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 topic seems classic, DP problem, direct five-D dp,dp[i1][j1][i2][j2][k] represents the minimum value from (i1,j1) to (i2,j2) cutting k times. The code is as follows:
//The ━━━━━━ of gods and Beasts ━━━━━━//┏┓┏┓//┏┛┻━━━━━━━┛┻┓//┃┃//┃━┃//████━████┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃//┃┃//┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━━━━━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛////━━━━━━ Feel the ━━━━━━ of Meng Meng//author:whywhy//Created time:2015 July 18 Saturday 12:12 42 seconds//File name:1191.cpp#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;intmap1[Ten][Ten];intdp[Ten][Ten][Ten][Ten][ -];intSumintI1,intJ1,intI2,intJ2) { intRet=map1[i2][j2]-map1[i1-1][j2]-map1[i2][j1-1]+map1[i1-1][j1-1]; RET*=ret; returnret;}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); intN; intMinn; scanf ("%d",&N); for(intI=1; i<=8;++i) for(intj=1; j<=8;++j) scanf ("%d",&Map1[i][j]); for(intI=1; i<=8;++i) for(intj=1; j<=8;++j) Map1[i][j]+=map1[i][j-1]; for(intj=1; j<=8;++j) for(intI=1; i<=8;++i) map1[i][j]+=map1[i-1][j]; for(inti1=1; i1<=8;++i1) for(intj1=1; j1<=8;++J1) for(inti2=i1;i2<=8;++i2) for(intj2=j1;j2<=8;++J2) dp[i1][j1][i2][j2][0]=sum (I1,J1,I2,J2); for(intk=1; k<n;++k) for(inti1=1; i1<=8;++i1) for(intj1=1; j1<=8;++J1) for(inti2=i1;i2<=8;++i2) for(intj2=j1;j2<=8;++J2) {Minn=0x3f3f3f3f; for(intq=i1;q<i2;++q) Minn=min (Minn,min (dp[i1][j1][q][j2][k-1]+sum (q+1, j1,i2,j2), dp[q+1][j1][i2][j2][k-1]+sum (i1,j1,q,j2)); for(intq=j1;q<j2;++q) Minn=min (Minn,min (dp[i1][j1][i2][q][k-1]+sum (i1,q+1, i2,j2), dp[i1][q+1][i2][j2][k-1]+sum (i1,j1,i2,q)); DP[I1][J1][I2][J2][K]=Minn; } Long DoubleAns= (Long Double) (dp[1][1][8][8][n-1]); printf ("%.3f\n", sqrt (ans/n-(Long Double) map1[8][8]*map1[8][8]*1.0/((Long Double) (N) *N))) ; return 0;}
View Code
Medium POJ 1191 Checkerboard Split, DP.