Number of squares

Source: Internet
Author: User
Number of squares time limit: 3000/1000 ms (Java/other) memory limit: 65535/32768 K (Java/other) total submission (s): 8 accepted submission (s): 5 Font: times New Roman | verdana | georgiafont size: Regular → Problem description contains a positive integer in the square of N * n, and 0 in the square of other squares, see the following table:
0 0 0 0 0 0 0
0 0 13 0 0 6 0 0
0 0 0 0 7 0 0 0
0 0 0 14 0 0 0
0 21 0 0 0 4 0 0
0 0 15 0 0 0 0
0 14 0 0 0 0 0
0 0 0 0 0 0 0
You can start from the upper left corner of a person, and walk down or to the right until the bottom right corner is reached. On the way he walked, he could take away the number from the square (the square after the square is removed will become a number 0 ).
This person walked two times from the upper left corner to the lower right corner, trying to find two such paths so that the obtained number is the largest. There are multiple groups of input data. The first behavior of each group of data is an integer n (n <= 10), indicating the square map of N * n.
The next row has three integers. The first is the number of row numbers, the second is the number of column numbers, and the third is the number of rows in the row and column. A row of 0 0 0 indicates the end. For each input group, an integer is output to indicate the maximum sum obtained from the two paths. Sample Input
82 3 132 6 63 5 74 4 145 2 215 6 46 3 157 2 140 0 0
Sample output
67
Authorhynu


// Two-step multi-process DP

DP [X1, Y1, X2, y2] indicates that the first path is (x1, Y1), and the second path is (X2, Y2, we can naturally get the state transition equation:

Set P = max (sum [i1-1, J1, i2-1, J2], sum [i1-1, J1, I2, j2-1], sum [I1, j1-1, i2-1, J2], sum [I1, j1-1, I2, j2-1])
DP [X1] [Y1] [X2] [y2] = 0 (X1 | Y1 | X2 | y2 = 0)
= P + data [X1] [Y1] (x1 = X2 & Y1 = Y2)
= P + data [X1] [Y1] + data [X2] [y2] (x1! = X2 | Y1! = Y2)

#include<cstdio>#include<cstring>int dp[15][15][15][15],p[15][15];int main(){    int n,i1,j1,i2,j2,x,y,z;    while(scanf("%d",&n)!=EOF)    {        memset(dp,0,sizeof(dp));        memset(p,0,sizeof(p));        while(1)        {            scanf("%d%d%d",&x,&y,&z);            if(x+y+z==0) break;            p[x][y]=z;        }        for(i1=1;i1<=n;i1++)            for(j1=1;j1<=n;j1++)                for(i2=1;i2<=n;i2++)                    for(j2=1;j2<=n;j2++)                    {                        if(dp[i1-1][j1][i2-1][j2]>dp[i1][j1][i2][j2])                            dp[i1][j1][i2][j2]=dp[i1-1][j1][i2-1][j2];                        if(dp[i1-1][j1][i2][j2-1]>dp[i1][j1][i2][j2])                            dp[i1][j1][i2][j2]=dp[i1-1][j1][i2][j2-1];                        if(dp[i1][j1-1][i2-1][j2]>dp[i1][j1][i2][j2])                            dp[i1][j1][i2][j2]=dp[i1][j1-1][i2-1][j2];                        if(dp[i1][j1-1][i2][j2-1]>dp[i1][j1][i2][j2])                            dp[i1][j1][i2][j2]=dp[i1][j1-1][i2][j2-1];                        dp[i1][j1][i2][j2]+=p[i1][j1];                        if(i1!=i2||j1!=j2)                            dp[i1][j1][i2][j2]+=p[i2][j2];                    }        printf("%d\n",dp[n][n][n][n]);    }    return 0;}


 

 

Number of squares

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.