Number of three Squares

Source: Internet
Author: User

Description

With a n*n chart, we fill some of these squares with positive integers,
And the other squares are put in 0.

Someone from the top left corner of the picture, you can go down, or to the right, until you reach the lower right corner.

On the way, he took away the number of squares. (The number in the box becomes 0 when taken away)
This person from the upper left to the lower right corner of a total of 3 times, try to find out 3 paths, so that the sum of the maximum number of obtained.

formatInput Format

First line: N (4<=n<=20)
Next to a n*n matrix, each element in the matrix is not more than 80, not less than 0

output Format

A row that represents the maximum sum.

Example 1sample input 1[copy] 
41 2 3 42 1 3 41 2 3 41 3 2 4
sample Output 1[copy] 
39
Limit

Each test point 1s

Tips

Multi-Process DP

F[STEP][X1][X2][X3] indicates that the optimal solution of the three points is taken x1,x2,x3 when the step is reached.

Because only move down or to the right, so when moving the number of steps, you can move to the grid is a diagonal, so the enumeration three times moved to the horizontal axis, you can O (1) to calculate the ordinate, note that each square can only be taken once.

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6#include <algorithm>7 using namespacestd;8 Const intmaxn= -;9 intf[maxn*2][MAXN][MAXN][MAXN];Ten intMAP[MAXN][MAXN]; One intN; A intMain () { -      -scanf"%d",&N); the      for(intI=1; i<=n;i++){ -          for(intj=1; j<=n;j++){ -scanf"%d",&map[i][j]); -         } +     } -f[1][1][1][1]=map[1][1]; +      for(intstep=2; step<=2*n-1; step++) {//The number of steps, assuming that it took one step to go A          for(intX1=max (1, step-n+1); X1<=min (n,step); x1++) {//x1,x2,x3 is to use step to walk to the horizontal axis of the x1,x2,x3 square, at              for(intX2=max (1, step-n+1); X2<=min (n,step); x2++) {//with min (), Max () is a way to keep the squares from crossing -                  for(intX3=max (1, step-n+1); X3<=min (n,step); x3++){ -                     intdelta=map[x1][step-x1+1]+map[x2][step-x2+1]+map[x3][step-x3+1]; -                     if(X1==X2) delta-=map[x1][step-x1+1]; -                     if(X1==X3) delta-=map[x1][step-x1+1]; -                     if(X2==X3) delta-=map[x2][step-x2+1]; in                     if(X1==X2&AMP;&AMP;X1==X3) delta+=map[x1][step-x1+1];//minus one more time. -                      toF[step][x1][x2][x3]=max (f[step-1][x1][x2][x3],max (f[step-1][x1-1][x2][x3], +Max (f[step-1][x1][x2-1][x3],max (f[step-1][x1][x2][x3-1], -Max (f[step-1][x1-1][x2-1][x3],max (f[step-1][x1-1][x2][x3-1], theMax (f[step-1][x1][x2-1][x3-1],f[step-1][x1-1][x2-1][x3-1])))))))+Delta; *                      $                     Panax Notoginseng                 } -             } the         } +     } Acout<<f[2*n-1][n][n][n]; the     return 0; +}

Number of three 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.