Checkerboard Segmentation
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 12456 |
|
Accepted: 4389 |
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 '.
Input
The 1th Act is 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
Only one number is O ' (rounded to three digits after the decimal point).
Sample Input
31 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 0 3
Sample Output
1.633
Source
Noi 99
Rujia Black Book, method book written in very detailed, do not repeat, this should be considered a kind of two-dimensional interval DP bar
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring># Include <iostream> #include <algorithm>using namespace std;const int inf = 0x3f3f3f3f;int DP[20][10][10][10] [10];int d[10][10][10][10];int mat[20][20];int sum[20][20];int main () {int n;while (~scanf ("%d", &n)) {double _x = 0; memset (sum, 0, sizeof (sum)), memset (DP, INF, sizeof (DP)), for (int i = 1; I <= 8; ++i) {for (int j = 1; J <= 8; ++j) { scanf ("%d", &mat[i][j]);} for (int j = 1; J <= 8; ++j) {Sum[i][j] + sum[i][j-1] + mat[i][j];}} for (int j = 1; J <= 8; ++j)//preprocessing {for (int i = 1; I <= 8; ++i) {Sum[i][j] + = Sum[i-1][j];}} for (int i = 1, i <= 8; ++i) {for (int j = 1, J <= 8; ++j) {for (int k = i, K <= 8; ++k) {for (int l = j; l <= 8; ++L) {D[i][j][k][l] = Sum[k][l]-sum[k][j-1]-sum[i-1][l] + sum[i-1][j-1];//printf ("D[%d] [%d] [%d] [%d] =%d\n ", I, J, K, L, D[i][j][k][l]);D [i][j][k][l] *= d[i][j][k][l];}}} _x = sum[8][8] * 1.0/n;_x *= _x;for (int i = 1; I <= 8; ++i) {for (int j = 1; J <= 8; ++j) {for (int k = i; k <= 8; ++k) {for (int l = j; l <= 8; ++l) {dp[0][i][j][k][l] = D[i][j][k][l];}}} for (int i = 1, i < n; ++i) {for (int j = 1; J <= 8; ++j)/*x1*/{for (int k = 1; k <= 8; ++k)/*y1*/{for (int l = j; L <= 8; ++L)/*x2*/{for (int p = k; p <= 8; ++p)/*y2*/{for (int q = j; q < L; ++q) {dp[i][j][k][l][p] = min (min (dp[i][j][k][l) [P], dp[i-1][j][k][q][p] + d[q + 1][k][l][p]), Dp[i-1][q + 1][k][l][p] + d[j][k][q][p]);} for (int q = k; q < P; ++q) {dp[i][j][k][l][p] = min (min (dp[i][j][k][l][p], Dp[i-1][j][k][l][q] + d[j][q + 1][l][p]), Dp[i-1][j][q + 1][l][p] + d[j][k][l][q]);}}}}} Double T = dp[n-1][1][1][8][8] * 1.0/n;printf ("%.3f\n", sqrt (T-_x));} return 0;}
poj1191--Checkerboard Segmentation