POJ 1191 board Segmentation

Source: Internet
Author: User

Board partitioning
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 11013 Accepted: 3869

Description

Split an 8*8 checker as follows: Cut the original checker down a rectangle checker and make the remaining part as a rectangle. Then, split the remaining part so that (n-1) is cut) after that, there are n rectangular checks in total with the remaining rectangular checkerboard. (Each cut can only be performed along the edges of the checker lattice)

 


 

Each grid on the original Board has a score. The total score of a rectangle board is the sum of the scores of each grid. Now we need to divide the checkerboard into n rectangular checkerboards according to the above rules, and minimize the mean variance of the total score of Each rectangular checkerboard.
Mean Variance. The average value and xi are the total score of the I-th rectangle board.
Program the given chessboard and n to find the minimum value of O.

Input

1st act as an integer n (1 <n <15 ).
Each row contains eight non-negative integers less than 2nd, indicating the score of the corresponding grid on the board. Separate two adjacent numbers in each line with a space.

Output

Only one number, which is o'' (rounded to the last three digits after the decimal point ).
Sample Input

3
1 1 1 1 1 1 3
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 0
1 1 1 1 1 0 3 Sample Output

1.633 Source

Noi 99
My transfer equation:
// Horizontal cut
Dp [k] [x1] [y1] [x2] [y2] = min (dp [k] [x1] [y1] [x2] [y2], dp [1] [x1] [y1] [t] [y2] + dp [k-1] [t + 1] [y1] [x2] [y2]);
Dp [k] [x1] [y1] [x2] [y2] = min (dp [k] [x1] [y1] [x2] [y2], dp [k-1] [x1] [y1] [t] [y2] + dp [1] [t + 1] [y1] [x2] [y2]);
// Longitudinal Cutting
Dp [k] [x1] [y1] [x2] [y2] = min (dp [k] [x1] [y1] [x2] [y2], dp [1] [x1] [y1] [x2] [t] + dp [k-1] [x1] [t + 1] [x2] [y2]);
Dp [k] [x1] [y1] [x2] [y2] = min (dp [k] [x1] [y1] [x2] [y2], dp [k-1] [x1] [y1] [x2] [t] + dp [1] [x1] [t + 1] [x2] [y2]);

#include <iostream>   #include <cstdio>   #include <cstring>   #include <cmath>   #define N 20   #define INF 0x7ffffff   #define M 8   using namespace std;  int sum[N][N][N][N],dp[N][N][N][N][N];  int a[N][N];  int main()  {      //freopen("data.in","r",stdin);       int n;      while(scanf("%d",&n)!=EOF)      {          for(int i=0;i<=M-1;i++)          {              for(int j=0;j<=M-1;j++)              {                  scanf("%d",&a[i][j]);              }          }          for(int x1=0;x1<=M-1;x1++)          {              for(int y1=0;y1<=M-1;y1++)              {                  for(int x2=x1;x2<=M-1;x2++)                  {                      for(int y2=y1;y2<=M-1;y2++)                      {                          if(y2==y1&&x2==x1)                          {                              sum[x1][y1][x2][y2] = a[x1][y1];                          }else if(x2==x1)                          {                              sum[x1][y1][x2][y2] = sum[x1][y1][x2][y2-1]+a[x2][y2];                          }else if(y2==y1)                          {                              sum[x1][y1][x2][y2] = sum[x1][y1][x2-1][y2] + a[x2][y2];                          }else                          {                              sum[x1][y1][x2][y2]=sum[x1][y1][x2][y2-1]+sum[x1][y1][x2-1][y2]-sum[x1][y1][x2-1][y2-1]+a[x2][y2];                          }                          dp[1][x1][y1][x2][y2] = sum[x1][y1][x2][y2]*sum[x1][y1][x2][y2];                      }                  }              }          }          for(int k=2;k<=n;k++)          {              for(int x1=0;x1<=M-1;x1++)              {                  for(int y1=0;y1<=M-1;y1++)                  {                      for(int x2=x1;x2<=M-1;x2++)                      {                          for(int y2=y1;y2<=M-1;y2++)                          {                              dp[k][x1][y1][x2][y2]=INF;                              for(int t = x1;t<=x2-1;t++) //hor                               {                                  dp[k][x1][y1][x2][y2]=min(dp[k][x1][y1][x2][y2],dp[1][x1][y1][t][y2]+dp[k-1][t+1][y1][x2][y2]);                                  dp[k][x1][y1][x2][y2]=min(dp[k][x1][y1][x2][y2],dp[k-1][x1][y1][t][y2]+dp[1][t+1][y1][x2][y2]);                              }                              for(int t = y1;t<=y2-1;t++) //ver                               {                                  dp[k][x1][y1][x2][y2]=min(dp[k][x1][y1][x2][y2],dp[1][x1][y1][x2][t]+dp[k-1][x1][t+1][x2][y2]);                                  dp[k][x1][y1][x2][y2]=min(dp[k][x1][y1][x2][y2],dp[k-1][x1][y1][x2][t]+dp[1][x1][t+1][x2][y2]);                              }                          }                      }                  }              }          }          double temp = (double)(sum[0][0][M-1][M-1])/(double)(n);          double res1 = temp*temp;          double res = (double)(dp[n][0][0][M-1][M-1])/(double)n - res1;          res = sqrt(res);          res = res*1000;          res+=0.5;          int R = (int)(res);          res = (double)(R)/1000;          printf("%.3lf\n",res);      }      return 0;  }  

 

Related Article

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.