Beginner DP (2) "Checkerboard Segmentation" in Black Book

Source: Internet
Author: User

Test Instructions: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 '.


Analysis:will be formulated Jane can getσ 2= 1/n*∑xi2-ˉx,that is, you just need to make∑xi2The smallest can;we use Dp[a][i][j][k][s] to represent the minimum value of the first Cut (i,j,k,s) checkerboard;Then there areMinmin=min (Minmin,min (DP[A-1][I][J][M][S]+T*T,DP[A-1][M+1][J][K][S]+T2*T2));    (i<=m<k) Minmin=min (Minmin,min (DP[A-1][I][J][K][M]+T*T,DP[A-1][I][M+1][K][S]+T2*T2)); (J<=m<s)dp[a][i][j][k][s]=minmin;

poj1191:http://poj.org/problem?id=1191 Reference Code:
#include <iostream> #include <iomanip> #include <cmath>using namespace std; #define INF 0xffffffint n,a [10] [10],sum[10][10];int dp[15][10][10][10][10];int countsum (int x1,int y1,int x2,int y2) {return sum[x2][y2]-sum[x2][y1-1 ]-sum[x1-1][y2]+sum[x1-1][y1-1];} int fun (int k) {//Initialize for (int i=0;i<8;i++) for (int j=0;j<8;j++) for (int. k=0;k<8;k++) for (int s=0;s<8;s++) {int T=countsum (i,j,k,s);DP [0][i][j][k][s]=t*t;} for (int a=1;a<=k;a++) {for (int. i=0;i<8;i++) {for (int. j=0;j<8;j++) {for (int k=0;k<8;k++) {for (int s=0;s<8) { ; s++) {int minmin = inf;if ((k-i+1) * (s-j+1) <a+1) {dp[a][i][j][k][s]=minmin;continue;} for (int m=i;m<k;m++) {int t=countsum (m+1,j,k,s); int t2=countsum (i,j,m,s); Minmin=min (Minmin,min (dp[a-1][i][j][m) [S]+t*t,dp[a-1][m+1][j][k][s]+t2*t2)];} for (int m=j;m<s;m++) {int t=countsum (i,m+1,k,s); int t2=countsum (I,J,K,M); Minmin=min (Minmin,min (dp[a-1][i][j][k) [M]+t*t,dp[a-1][i][m+1][k][s]+t2*t2)];} Dp[a][i][j][k][s]=minmin;}}}} Return dp[k][0][0][7][7];} int main () {while (cin>>n) {for (int i=0;i<8;i++) {int s=0;for (int j=0;j<8;j++) {cin>>a[i][j];s+=a[i][ J];if (i==0) sum[i][j]=s;elsesum[i][j]=sum[i-1][j]+s;}} Double ans = n*fun (n-1)-sum[7][7]*sum[7][7];cout<<fixed<<setprecision (3) <<sqrt (ans/(n*n)) < <endl;} return 0;}
submit the answer in C + + is WA with g++ on AC ...

Beginner DP (2) "Checkerboard Segmentation" in Black Book

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.