Poj 1191 DP + DFS board Segmentation

Source: Internet
Author: User

Question:

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.

 

ApplicationDynamic Planning,StatusInt D (int K, int X1, int Y1, int X2, int Y2), indicating that the upper left corner is (x1, Y1), and the lower right corner is (X2, Y2) the Minimum Sum of squares that can be achieved when the matrix is cut into N blocks.
State transition equationIs

Nowstate = min (nowstate, DFS (K-1, X1, Y1, A, Y2) + s [A + 1, Y1, X2, y2])

Nowstate = min (nowstate, DFS (K-1, A + 1, Y1, X2, Y2) + s [X1, Y1, A, y2])

Nowstate = min (nowstate, DFS (K-1, X1, B + 1, X2, Y2) + s [X1, Y1, X2, B])

Nowstate = min (nowstate, DFS (K-1, X1, Y1, X2, B) + s [X1, B + 1, X2, y2])

 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 int dp[15][9][9][9][9],val[9][9],n; 8  9 int sum(int x1,int y1,int x2,int y2)10 {11     int ans=val[x2][y2]-val[x1-1][y2]-val[x2][y1-1]+val[x1-1][y1-1];12     return ans*ans;13 }14 15 int dfs(int k,int x1,int y1,int x2,int y2)16 {17     if(dp[k][x1][y1][x2][y2]) return dp[k][x1][y1][x2][y2];18     if(k==1||x1==x2||y1==y2){19         dp[k][x1][y1][x2][y2]= sum(x1,y1,x2,y2);20         return dp[k][x1][y1][x2][y2];21     }22     int nowstate=INF;23     for(int i=x1;i<x2;i++){24         nowstate=min(nowstate,dfs(k-1,i+1,y1,x2,y2)+sum(x1,y1,i,y2));25         nowstate=min(nowstate,dfs(k-1,x1,y1,i,y2)+sum(i+1,y1,x2,y2));26     }27     for(int i=y1;i<y2;i++){28         nowstate=min(nowstate,dfs(k-1,x1,i+1,x2,y2)+sum(x1,y1,x2,i));29         nowstate=min(nowstate,dfs(k-1,x1,y1,x2,i)+sum(x1,i+1,x2,y2));30     }31     dp[k][x1][y1][x2][y2]=nowstate;32     return nowstate;33 }34 35 void change()36 {37     for(int i=0;i<=8;i++)38         val[i][0]=0,val[0][i]=0;39 40     for(int i=1;i<=8;i++)41         for(int j=1;j<=8;j++)42             val[i][j]+=val[i-1][j]+val[i][j-1]-val[i-1][j-1];43 }44 45 int main()46 {47     while(~scanf("%d",&n)){48 49         memset(dp,0,sizeof(dp));50 51         for(int i=1;i<=8;i++)52             for(int j=1;j<=8;j++)53                 scanf("%d",&val[i][j]);54 55         change();56 57         double res;58         double ave=1.0*val[8][8]/n;59         res=sqrt((double)dfs(n,1,1,8,8)/n-ave*ave);60         printf("%.3f\n", res);61     }62     return 0;63 }

 

 

Poj 1191 DP + DFS board Segmentation

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.