P1004 and p1004 squares

Source: Internet
Author: User

P1004 and p1004 squares
Description

The square plot with N * N (N <= 9) is filled with positive integers in some squares, while in other squares

The number of people is 0. As shown in (see example ):

A 0  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 0 21  0  0  0  4  0  0 0  0 15  0  0  0  0  0 0 14  0  0  0  0  0  0 0  0  0  0  0  0  0  0.                       B

Someone starts from the point in the upper left corner of the graph and can walk down or right until B in the lower right corner.

Point. 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 takes two steps from A.M. To a.m. and tries to find two such paths to maximize the sum of the obtained numbers.

Input/Output Format Input Format:

The first line of the input is an integer N (representing the square map of N * N). The next line has three integers, the first two

Represents the position. The third number is the number placed on the position. A single row of 0 indicates that the input is complete.

Output Format:

Only one integer is output, indicating the maximum sum obtained from the two paths.

Input and Output sample Input example #1:
82 3 132 6  63 5  74 4 145 2 215 6  46 3 157 2 140 0  0
Output sample #1:
67
Description

NOIP 2000 raise group fourth question

 

There are four scenarios:
① Both roads reach this point from the top

② Both roads arrive at this point from the left

③ The first route arrives at this point from the left and the second route arrives at this point.

④ The first route arrives at this point from the top, and the second route arrives at this point on the left.

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 void read(int & n) 7 { 8     char c='+';int x=0;     9     while(c<'0'||c>'9')c=getchar();10     while(c>='0'&&c<='9')11     {12         x=x*10+c-48;13         c=getchar();14     }15     n=x;16 }17 const int MAXN=10;18 int a[MAXN][MAXN];19 int dp[MAXN][MAXN][MAXN][MAXN];20 int main()21 {22     //ios::sync_with_stdio(false);23     int n;24     read(n);25     while(1)26     {27         int x,y,z;28         read(x);read(y);read(z);29         if(x==0&&y==0&&z==0)30         break;31         a[x][y]=z;32     }33         34 35     for(int i=1;i<=n;i++)36         for(int j=1;j<=n;j++)37             for(int k=1;k<=n;k++)38                 for(int l=1;l<=n;l++)39                 {40                     if(i+j!=k+l)continue;41                     dp[i][j][k][l]=max(max(dp[i-1][j][k-1][l],42                                        dp[i-1][j][k][l-1]),43                                        max(dp[i][j-1][k-1][l],44                                        dp[i][j-1][k][l-1]))+a[i][j]+a[k][l];45                     if(i==k&&j==l)46                     dp[i][j][k][l]-=a[i][j];47                 }48     printf("%d",dp[n][n][n][n]);49     return 0;50 }

 

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.