Algorithm training squares to take several blue bridge cups

Source: Internet
Author: User
Tags min
Problem description
With N*n (n<=10), we fill some of the squares with positive integers, while the other squares put the number 0.
A person from the upper left corner of the figure (a point), you can walk down, or to the right, until you reach the lower right corner of Point B (n,n). 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.
Input format
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.
Output format
Just output an integer that represents the maximum and the 2 paths that are obtained.
Sample input
8
2 3 13
2 6 6
3 5 7
4 4 14
5 2 21
5 6 4
6 3 15
7 2 14
0 0 0
Sample output
67


Ideas

A person walking two times can be seen as two people walking at the same time, here are two points:

X1+y1=x2+y2 that is to say that these two people side-by a diagonal line

In order to and Max, the two men can not walk across the same lattice except for the opening ending.


Blue Bridge OJ The last test data seems to have a problem; the recursive formula in the SAC does not conform to the optimal substructure, because F (i-1,j,k ") is not the largest, but the two V value is large enough to obtain the optimal solution.

#include <stdio.h> #define MAX2 (A, b) a>b?a:b #define MIN2 (A, b) a<b?a:b int a[100][100]={0};//store table int N; int ma In () {int x,y,i,j,k,q,t,max,min,x1,x2; int dp[20][20][20][20]={0};//stores the value and maximum value of the path from two to the end of int yb[2],yc[2],xb[2],xc[2
];
scanf ("%d", &n);
    while (1) {scanf ("%d%d", &x,&y);
    scanf ("%d", &a[x][y]);
if (x==0&&y==0&&a[x][y]==0) break;
} Dp[n][n][n][n]=a[n][n];
    for (i=2*n-1;i>=2;i--) {//two points are always on the same line, i=x+y the table as a diagonal max=min2 (n,i-1);
    MIN=MAX2 (1,I-N); for (x1=min;x1<=max;x1++) {for (x2=min;x2<=max;x2++) {//traversal on this slash, any combination of two points if (x1==x2&&! (
            X1==1&&x2==1)) continue;
            yb[0]=i-x1+1;
            yb[1]=i-x1;
            xb[0]=x1;
            xb[1]=x1+1;
            yc[0]=i-x2+1;
            yc[1]=i-x2;
            xc[0]=x2;
            xc[1]=x2+1;
            q=0; for (j=0;j<=1;j++) {for (k=0;k<=1;k++) {if (Xb[j]==xc[k]) continue;//withLines, x different, will not overlap if (q< (T=a[x1][i-x1]+a[x2][i-x2]+dp[xb[j]][yb[j]][xc[k]][yc[k]))) {q=t;
        }}} dp[x1][i-x1][x2][i-x2]=q;
}}} Dp[1][1][1][1]-=a[1][1];
printf ("%d", dp[1][1][1][1]);
return 0;
 }



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.