Codevs1043 [Check number of squares]

Source: Internet
Author: User

A square chart with n*n (n<=10, we fill in some of these squares with positive integers, while the other squares put the number 0. As shown in the following figure (see example):

Someone from a point in the upper left corner of the picture, you can walk down, or to the right, until you reach the lower right corner of point B. On the way, he can take the number in the squares (the squares will change to the number 0).

This person from point A to point B to walk two times, try to find out 2 such paths, so that the sum of the obtained number is the largest.

Enter a description input Description
The first behavior of the input is an integer n (a square chart representing n*n), followed by three integers per line, the first two representing the position, and the third number as the number placed on that position. A single line of 0 indicates the end of the input.

Outputs description Output Description
Just output an integer that represents the maximum and the 2 paths that are obtained.

Sample input to sample
8

  2  3

  2  6   6

  3  5   7

  4 4

  5  2  5 6   4

  6 3  (

  7 2  )

  0 0  0

Sample output Sample Outputs
The problem: with DP, and the usual checkerboard DP is the question is two pieces go together. Dp[i][j][k][l] Indicates the maximum number of the first piece in (I,j) and the second at (k,l).

 #include <cstdio> #include <iostream> using namespace std; int a[15][15][15][15];
int mp[15][15];
    int main () {int n;
    scanf ("%d", &n);
    int x = 1, b = 1, C;
        for (; x+b;) {scanf ("%d%d%d", &x, &b, &c);       
    MP[X][B] = c;
          } for (int i = 1, i <= N; i++) for (int j = 1; J <= N; j + +) for (int k = 1; k <= N; k++) for (int l = 1; l <= N; l++) {a[i][j][k][l] + = MAX (max (A[i-1][j][k-1][l], a[i-1][j][k][l-1]),
              Max (A[i][j-1][k-1][l], a[i][j-1][k][l-1]));
              A[i][j][k][l] + = Mp[i][j] + mp[k][l]; 
          if (i = = k && J = = L) a[i][j][k][l]-= mp[i][j];
} printf ("%d", a[n][n][n][n]); }

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.