Nyoj 61 passed the paper and nyoj 712 explored the double process dp

Source: Internet
Author: User

The question is that a robot starts from 1.1 to reach m.. You can only go to the right or up when you go back. You can only go up or up to the left when you come back, but one point can only go once. Each point along the way has a treasure, ask how much valuable the treasure can be retrieved.

This problem is equivalent to two robots starting from 1.1. at, the speed of the two robots is certain, so each time the two machines cannot go to the same grid (except for reaching m. n ).

If you do not want the two robots to go to the same point, you only need to at any time. The number of rows where robot 1 is located is not equal to the number of rows where robot 2 is located. Because the speed is the same, the starting point is the same. If the two robots arrive at the same row at k at a certain time point, their columns will inevitably be the same, so they only need to be in different rows,

Set f [k] [I] [j] to the greatest value of the treasure collected by step k, step 1, and step 2 to step j, then f [k] [I] [j] = max (f [k-1] [I-1] [j], f [k-1] [I] [J-1], f [k-1] [I] [j], f [k-1] [I-1] [J-1]) + a [I] [k + 2-i] + a [j] [k + 2-j];

If the No. 1 robot takes the route on the left and the No. 2 robot takes the route on the right, because the route does not want to be handed in, the first step of the No. 1 robot is down, the number 2 is right, and the Number 1 does not reach column n before the end, and the number 2 does not reach Row m, then we need to consider the value range of I and j. We must make sure that the two rows are between (1, m), and the columns are between (1, n, if k + 1 <m, the number of rows is smaller than m,

We can also consider this. Because each step forward of the robot has only one row and one column that will increase by 1, we can consider that the value of k represents the sum of the rows and columns, it is easy to understand. Because the two lines must be at the same end point, we can not let it reach the end point, that is, k <m + n; the state transition equation becomes f [k] [I] [j] = max (f [k-1] [I-1] [j], f [k-1] [I] [J-1], f [k-1] [I] [j], f [k-1] [I-1] [J-1]) + a [I] [k-I] + a [j] [k-j]; (k> = 3 & k <m + n );

 


The reference code is as follows;


[Cpp]
# Include <iostream>
# Include <cstring>
# Define MAX_SIZE 51
Using namespace std;
Int f [2 * MAX_SIZE + 10] [MAX_SIZE + 10] [MAX_SIZE + 10];
Int a [MAX_SIZE + 10] [MAX_SIZE + 10];
Int m, n;
Int max (int a, int B)
{
If (a> B)
Return;
Return B;
}
Void execute ()
{
If (m = 1 | n = 1)
{
Cout <0 <endl;
Return;
}
Memset (f, 0x80, sizeof (f ));
Int k, I, j;
F [2] [1] [1] = 0;
For (k = 3; k <m + n; k ++)
{
For (I = 2; I <k & I <= m; I ++)
{
If (k-I> = n | k-I <1)
Continue;
For (j = 1; j <K-1 & j <= s-1; j ++)
{
If (I = j)
Continue;
If (k-j> n | k-j <= 1)
Continue;
F [k] [I] [j] = max (f [k-1] [I-1] [j], f [k-1] [I-1] [J-1]), max (f [k-1] [I] [J-1], f [k-1] [I] [j]);
F [k] [I] [j] = f [k] [I] [j] + a [I] [k-I] + a [j] [k-j ];
}
}
}
Int ans = f [m + n-1] [m] [s-1] + a [m] [n];
Cout <ans <endl;
 
}
Int main ()
{
Int T;
Cin> T;
While (T --)
{
Cin> m> n;
For (int I = 1; I <= m; I ++)
For (int j = 1; j <= n; j ++)
Cin> a [I] [j];
Execute ();
}
Return 0;
}

# Include <iostream>
# Include <cstring>
# Define MAX_SIZE 51
Using namespace std;
Int f [2 * MAX_SIZE + 10] [MAX_SIZE + 10] [MAX_SIZE + 10];
Int a [MAX_SIZE + 10] [MAX_SIZE + 10];
Int m, n;
Int max (int a, int B)
{
If (a> B)
Return;
Return B;
}
Void execute ()
{
If (m = 1 | n = 1)
{
Cout <0 <endl;
Return;
}
Memset (f, 0x80, sizeof (f ));
Int k, I, j;
F [2] [1] [1] = 0;
For (k = 3; k <m + n; k ++)
{
For (I = 2; I <k & I <= m; I ++)
{
If (k-I> = n | k-I <1)
Continue;
For (j = 1; j <K-1 & j <= s-1; j ++)
{
If (I = j)
Continue;
If (k-j> n | k-j <= 1)
Continue;
F [k] [I] [j] = max (f [k-1] [I-1] [j], f [k-1] [I-1] [J-1]), max (f [k-1] [I] [J-1], f [k-1] [I] [j]);
F [k] [I] [j] = f [k] [I] [j] + a [I] [k-I] + a [j] [k-j ];
}
}
}
Int ans = f [m + n-1] [m] [s-1] + a [m] [n];
Cout <ans <endl;

}
Int main ()
{
Int T;
Cin> T;
While (T --)
{
Cin> m> n;
For (int I = 1; I <= m; I ++)
For (int j = 1; j <= n; j ++)
Cin> a [I] [j];
Execute ();
}
Return 0;
}

 

Related Article

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.