"POJ 1191" Checkerboard Split (DP)

Source: Internet
Author: User

"POJ 1191" Checkerboard Split (DP)

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13811 Accepted: 4917

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 checkerboard Segmentation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13811 Accepted: 4917

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

[Submit]   [Go back]   [Status] [Discuss]

Home PageGo back to top, where the 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


Brush a lap and brush back to the most primitive things. In fact, it is not the end of the thing ... Dp...... Too many tricks, burning brain special ...

The topic is simple, rough.

8*8 a square, each point has a value (0~100) requires cutting n-1 times, each time only from the rest of the edge of cutting to the other side, the picture of the problem is very clear. Then ask the smallest variance ...


The variance formula is given. But if the dead stare at the formula. Want to be too long should also think not thick come of ...

Need to change shape. I borrowed some other blog's Derivation ~ ~

s^2 = 1/n∑ (xi-x) ^2

= 1/n (n*x^2 +∑xi^2-2*x∑xi)

= 1/n∑xi^2-x^2;

(http://blog.csdn.net/xymscau/article/details/6854410)


This should actually be a little bit of an idea ~ you will find that X (mean) is fixed (sum/n), as long as the ∑xi^2 as small as possible, the final result must be the minimum value. And can be found that s^2 will not appear negative, that is to say, ∑xi^2 is smaller than x^2, so you can safely DP. Or say, with confidence (memory) search, each time the cutting, continue to search the part to continue searching, the other part of the need to quickly give a value, so you can preprocess the square of all the interval, or each use of violent run can also.


It is important to note that you cut out n rectangles to cut n-1 knives.

Also, the DP array should remember to record the number of cutting knives. At first it was because there was no record, causing chaos, and eventually WA

In addition, the DP is initialized first when not initialized to the INF, because some part of the end result is the INF, that is, no solution, if initially initialized to the INF, and this is the memory of the return value of the judgment, will cause a time-out. Because the DP is still down in this state. The positive solution should be initialized in code 1, when the discovery is the first time to search for the state, immediately into the INF, and then search out the state minimum (if not legal, it happens to be the previous step assignment of the INF (Infinity))


The code is as follows:

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm > #include <map> #include <set> #define LL long long#define Pr pair<int,int> #define FREAD () freopen (" In.in "," R ", stdin) #define FWRITE () freopen (" Out.out "," w ", stdout) using namespace std;const int INF = 0x3f3f3f3f;const int MSZ = 10000;const int mod = 1e9+7;const double eps = 1e-8;double arg;int n;int mp[8][8];int dp[16][8][8][8][8];int sum[8][ 8][8][8];void Init () {for (int x1 = 0, x1 < 8; ++x1) for (int y1 = 0, y1 < 8; ++y1) for (int x2 = x1, x2 < 8; ++x2) for ( int y2 = y1; Y2 < 8; ++Y2) {int tmp = 0;for (int i = x1; I <= x2; ++i) for (int j = y1; j <= Y2; ++j) tmp + = Mp[i][j];sum[x1][y1][x2][y2] = TM p*tmp;//printf ("sum[%d][%d][%d][%d]:%d\n", X1,y1,x2,y2,sum[x1][y1][x2][y2]);}} int dfs (int s,int x1,int y1,int x2,int y2) {if (s = = N) return sum[x1][y1][x2][y2];if (x1 = = x2 && y1 = = y2) return inf;if (~dp[s][x1][y1][x2][y2]) return dp[s][x1][y1][x2][y2];DP [s] [X1] [Y1] [X2] [Y2] = inf;for (int i = x1; i < X2; ++i) {int tmp = MIN (dfs (S+1,X1,Y1,I,Y2) +sum[i+1][y1][x2][y2],sum[x1][y1][i][y2]+dfs (s) +1,i+1,y1,x2,y2));//printf ("(%d,%d), (%d,%d), (%d,%d), (%d,%d)%d\n", x1,y1,i,y2,i+1,y1,x2,y2,tmp);DP [s][x1][y1 ][x2][y2] = min (dp[s][x1][y1][x2][y2],tmp);} for (int i = y1; i < y2; ++i) {int tmp = MIN (dfs (s+1,x1,y1,x2,i) +sum[x1][i+1][x2][y2],sum[x1][y1][x2][i]+dfs (s+1,x1,i+1 , X2,y2));//printf ("(%d,%d), (%d,%d), (%d,%d), (%d,%d)%d\n", x1,y1,x2,i,x1,i+1,x2,y2,tmp);DP [S][x1][y1][x2][y2] = min (dp[s][x1][y1][x2][y2],tmp);} return dp[s][x1][y1][x2][y2];} int main () {//fread ();//fwrite (); while (~SCANF ("%d", &n)} {arg = 0;for (int i = 0; i < 8; ++i) for (int j = 0; J < 8; + +J) {scanf ("%d", &mp[i][j]), arg + = Mp[i][j];} Memset (Dp,-1,sizeof (DP)), init ();d FS (1,0,0,7,7),//printf ("%d\n", Dp[0][0][7][7]),//printf ("%f\n", Arg);p rintf ("%. 3f\n ", sqrt (dp[1)[0] [0] [7] [7]*1.0/n-arg/n* (arg*1.0/n)));} return 0;}





"POJ 1191" Checkerboard Split (DP)

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.