Checkerboard Segmentation
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 16263 |
|
Accepted: 5812 |
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
Test instructions
A 8*8 board that splits. Splits a rectangle into two at a time. The value of a rectangle is the sum of all the lattice values inside. Now you want to cut out n rectangles, hoping to get the minimum mean variance.
Ideas:
Let's start by simplifying the formula of mean variance
The mean is a fixed value, because each rectangle is the sum of the lattice values. So we find that the result is related to the sum of Xi squared. The smaller the Xi squared, the better.
For each lattice, it can be expressed in two coordinates (I, j) and (x, y), respectively, in the upper-left and lower-right corners of the rectangle.
A rectangle has two cutting methods, cut horizontally, and cut vertically, assuming that it is cut at K
When cut horizontally, the rectangle is divided into (i, j) (K, Y) and (k + 1, j) (X, y)
When cut vertically, the rectangle is divided into (i,j) (x,k) and (I, k+1) (x, y)
However, there is no way to transfer the state because the order of the rectangles is not known. So we can then introduce one-dimensional variables that represent the order of the individual.
So when I cut it, I get two rectangles, one of which should be a i+1 cut rectangle. The state transition equation can be obtained.
Because I push i+1, it may be a little easier to write with a memory search.
1 //#include <bits/stdc++.h>2#include <iostream>3#include <cmath>4#include <algorithm>5#include <stdio.h>6#include <cstring>7#include <vector>8#include <map>9#include <Set>Ten One #defineINF 0x3f3f3f3f A using namespacestd; -typedefLong LongLL; - the intN; - intboard[Ten][Ten], hang[Ten][Ten], dp[ -][Ten][Ten][Ten][Ten]; - - /*int getval (int i, int j, int x, int y) + { - int res = 0; + for (int k = i; k <= x; k++) { A res + = Hang[i][y]-hang[i][j-1]; at } - return res * RES; - }*/ - - intGetval (intIintJintXinty) - { in intres =0; - for(intA = i; a <= x; a++){ to for(intb = j; b <= y; b++){ +Res + =Board[a][b]; - } the } * returnRes *Res; $ }Panax Notoginseng - intDP (intKintIintJintXinty) the { + intAns =0; A if(Dp[k][i][j][x][y] >=0)returnDp[k][i][j][x][y]; the if(k = = n-1){ + returnGetval (I, J, X, y); - } $Dp[k][i][j][x][y] =inf; $ for(intTMP = i; TMP < x; tmp++){ -ans = min (DP (k +1, I, J, tmp, Y) + getval (tmp +1, j, X, y), DP (k +1, TMP +1, j, X, y) +Getval (i, J, tmp, y)); -Dp[k][i][j][x][y] =min (ans, dp[k][i][j][x][y]); the } - for(intTMP = j; tmp < Y; tmp++){Wuyians = min (DP (k +1, I, J, X, TMP) + getval (i, TMP +1, x, y), DP (k +1, I, TMP +1, x, y) +Getval (I, J, X, TMP)); theDp[k][i][j][x][y] =min (ans, dp[k][i][j][x][y]); - } Wu returnDp[k][i][j][x][y]; - } About $ intMain () { - - while(SCANF ("%d", &n)! =EOF) { - Doublesum =0; Amemset (Hang,0,sizeof(hang)); + for(inti =1; I <=8; i++){ the for(intj =1; J <=8; J + +){ -scanf"%d", &board[i][j]); $Sum + = board[i][j] *1.0; theHANG[I][J] = hang[i][j-1] +Board[i][j]; the } the } theSum/=1.0*N; -Memset (DP,-1,sizeof(DP)); in intAns = DP (0,1,1,8,8); the //cout<<ans<<endl; the //cout<<dp[n-1][1][1][8][8]<<endl; Aboutprintf"%.3f\n", sqrt ((dp[0][1][1][8][8])*1.0/n-sum*sum)); the } the return 0; the}
poj1191 Checkerboard Partition "interval DP" "Memory Search"