Rokua P1004 Check Number "multithreading dp/four-dimensional dp/"

Source: Internet
Author: User

Title Description (https://www.luogu.org/problemnew/show/1004)

With N*n (n<=9), we fill some of these squares with positive integers, while the other squares

Person number 0. As shown (see examples):

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 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 B

Point. 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/output format

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

Represents the position, and the third number is the number placed at 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.

Input and Output sample input sample #: Copy
3 6 (  5)  4 145 2 215 6  3 157 2 0  0
Output Example # #: Replication
67
Description

NOIP 2000 Improvement Group Fourth question

"Analysis":

1th: Open four-dimensional array:

Take two paths as two people at the same time,

There are four coordinates, respectively, for two persons.

Vertical and horizontal coordinates, with the same four for loop.

2nd: Decision-making:

There are four ways to go:

(bottom, bottom), (bottom, right),

(right, bottom), (right, right).

are represented as:

S[I-1][J][H-1][K],S[I][J-1][H][K-1]

S[I-1][J][H][K-1],S[I][J-1][H-1][K]

(I,j is the first person, H,k is the second person)

The state transition equation can be obtained:

First Person: S[i][j][h][k]=max (TMP1,TMP2) +a[i][j];

A second person: s[i][j][h][k]+=a[h][k];

Note: If i=h&&j=k, it can only be added once.

"Code":

#include <bits/stdc++.h>using namespacestd;intn,x,y,val,maxn,f[ A][ A][ A][ A],a[ A][ A];//A[i][j][k][l] means that two people go at the same time, a walk i,j a walk k,lintMain () {CIN>>N; Memset (A,0,sizeofa);  while(cin>>x>>y>>val) {        if(x==0&&y==0&&val==0) Break; A[x][y]=Val; }     for(intI=1; i<=n;i++){         for(intj=1; j<=n;j++){             for(intk=1; k<=n;k++){                 for(intL=1; l<=n;l++){                    intOp1=max (f[i-1][j][k-1][l],f[i][j-1][k][l-1]); intOp2=max (f[i-1][j][k][l-1],f[i][j-1][k-1][l]); F[I][J][K][L]=max (OP1,OP2) +a[i][j]+A[k][l]; if(i==k&&j==l) f[i][j][k][l]-=A[i][j]; } }}} printf ("%d\n", F[n][n][n][n]); return 0;}
four-dimensional DP

Rokua P1004 Check Number "multithreading dp/four-dimensional dp/"

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.