Searching for treasures--the sixth annual College student Program design competition in Henan province

Source: Internet
Author: User
Tags exit in

Title Description

Legend HMH There is a m*n maze in the great desert, with many treasures hidden in it. One day,Dr.kong found a maze of maps, he found in the maze everywhere there are treasures, the most precious treasures are hidden in the lower right corner, the maze of the entrance and exit in the upper left corner. Of course, the pathways in the maze are not flat and there are traps everywhere. Dr.kong decided to let his robot go on an expedition.

But when the robot is moving from the upper left to the lower right, it will only go down or go right. go up or left, and take away every treasure along the road.

Dr.kong wants his robot to make as many treasures as possible. please write a program to help dr.kong calculate How many treasures can be brought out by the most.

Input

First line: K indicates how many sets of test data are available.

Next to each set of test data:

Line 1 : M N

Line 2~m+1 : ai1 ai2 ... A in (I=1,... .., m)

2≤k≤5      ≤m, N 5 0      0≤aij≤1 .,M; J=1, ... ,n)

All data are integers. There is a space between the data.

Output

For each set of test data, the output is one line: The number of treasures the robot has to carry the most value

Sample input
22 30 10 1010 10 803 30 3 92 8 55 7 100
Sample output
120134

D[k][x1][y1][x2][y2]={max (d[k-1][x1-1][y1][x2][y2-1],
               D[k-1][x1-1][y1][x2-1][y2],
               D[K-1][X1][Y1-1][X2][Y2-1],
               D[k-1][x1][y1-1][x2-1][y2])
             +A[X1][Y1]+A[X2][Y2];
             };
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intd[ the][ the][ the],a[ the][ the];intMain () {intx,i,j,k,t; intn,m; scanf ("%d",&x);  while(x--) {scanf ("%d%d",&n,&m); memset (d,0,sizeof(d));  for(i=1; i<=n;++i) for(j=1; j<=m;++j) scanf ("%d",&A[i][j]);  for(k=3; k<n+m;k++)             for(i=1; i<=n;++i) for(j=i+1; j<=n;++j)//two lines must be a greater than the horizontal axis of the 21 bar                {                    if(k-i<1|| k-j<1)//There is a x+y=k known y=k-x so control the range of column y, the following similarly                         Break; if(k-i>m| | K-j>m)Continue; D[K][I][J]= Max (max (d[k-1][i-1][j],d[k-1][i-1][j-1]), Max (d[k-1][i][j-1],d[k-1][i][j])); //here is a 5-dimensional 3-dimensional abridged version, I and J are not coordinates, but two moving coordinates of the horizontal axis,//since the ordinate and horizontal axis are//linear function relationship, so it can be omitted. D[K][I][J] +=a[i][k-i]+a[j][k-J]; } t=n+m; D[t][n][n]= Max (max (d[t-1][n-1][n],d[t-1][n-1][n-1]), Max (d[t-1][n][n-1],d[n-1][n][n])); printf ("%d\n", d[t][n][n]+a[n][m]); }    return 0;}

Searching for treasures--the sixth annual College student Program design competition in Henan province

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.