lightoj1071_多线程dp

http://lightoj.com/volume_showproblem.php?problem=1071

All of you must have heard the name of Baker Vai. Yes, he rides a bike and likes to help people. That's why he is popular amongst general people.

Baker Vai lives in a city which can be modeled as a 2D m x n matrix. Where the north-west corner is cell 1, 1 and the south-east corner is cell m, n. In each cell there are certain amount of people who needs help which is already known to Baker Vai.

Each day Baker Vai starts his journey from the north-west corner and he can only go to east or south. This way he reaches the south-east corner of the city. After that he returns back to the north-west, but this time he can only move to west or north. He doesn't want a cell to be visited twice other than the two corners. And if he visits a cell, he helps all the people in the cell.

Now you are given the map of the city and the number of people who need help in all cells for a particular day. You have to help Baker Vai finding the maximum number of people he can help in that day.

Input

Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case contains a blank line and two integers, m, n (2 ≤ m, n ≤ 100). Each of the next m lines will contain n integers, denoting the number of people who are in need. In a cell there will be no more than 20 people and a cell can be empty, too.

Output

For each test case, print the case number and the maximum number of people Baker Vai can help considering the above conditions.

Sample Input

Output for Sample Input

2

3 3

1 1 1

1 0 1

1 1 1

3 4

1 1 0 1

1 1 1 1

0 1 10 1

Case 1: 8

Case 2: 18

和hdu2686一样

题意:从(1, 1)到(n, n)再到(1, 1).

右下到左上只能左, 上;左上到右下只能右,下。

思路:设两个人同时从(1, 1)出发,如果同时到(i, j),跳过

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 typedef long long LL;
15 
16 int a[110][110], dp[110][110][110];
17 int main()
18 {
19     int t, n, m;
20     scanf("%d", &t);
21     for(int ca = 1; ca <= t; ca++)
22     {
23         scanf("%d %d" , &n, &m);
24         for(int i = 1; i <= n; i++)
25             for(int j = 1; j <= m; j++)
26                 scanf("%d", &a[i][j]);
27         memset(dp, 0, sizeof(dp));
28         for(int i = 1; i <= n; i++)
29         {
30             for(int j = 1; j <= m; j++)
31             {
32                 if(i == n && j == m)
33                 {
34                     dp[i][j][n]=max(dp[i-1][j][n], dp[i][j-1][n-1])+a[n][m];
35                     break;
36                 }
37                 for(int k = 1; k < i + j && k <= n; k++)
38                 {
39                     if(i+j-k>m)
40                         continue;
41                     if(i == 1 && j == 1)
42                         dp[1][1][1] = a[1][1];
43                     else if(i == k)
44                         continue;
45                     else
46                     {
47                         dp[i][j][k] = max(max(dp[i-1][j][k],dp[i-1][j][k-1]), max(dp[i][j-1][k], dp[i][j-1][k-1]))+a[i][j]+a[k][i+j-k];
48                     }
49                 }
50             }
51         }
52         printf("Case %d: %d
", ca, dp[n][m][n]);
53     }
54     return 0;
55 }
原文地址:https://www.cnblogs.com/luomi/p/5958041.html