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)