light Oj 1071 - Baker Vai(记忆化搜索)

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

1071 - Baker Vai
    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

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

 题目大意:给出一个矩阵,两个人一块从左上角(1,1)位置走到右下角(n,m)位置,求经过的点上的数字之和最大是多少,每一步都只能往最接近右下角的位置走,即下一步的位置可以是(i+1,j)or (i, j+1),如果一个人已经走过了(i,j)位置,则另外一个人经过改点时该点上的位置就是0了

思路:记忆化搜索,dp[st][x1][x2]表示当前的和,st表示走的第几步,x1和x2分别为两个人所在的行,由步数和所在的行可以得到所在的位置

有四种情况

①当两人在同一行时,一个人往下走,一个人往右走

②当两人在同一行时,一个人往右走,一个人往下走

③当两个人不在同一行时,两个人都往下走

④当两个人不在同一行时,两个人都往右走

记忆化搜索

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>

using namespace std;

#define N 110
#define INF 0x3f3f3f3f
#define MOD
#define met(a, b) memset(a, b, sizeof(a))

typedef long long LL;

using namespace std;

int dp[2*N][N][N], a[N][N], n, m;

int DFS (int st, int x1, int x2)
{
    if (dp[st][x1][x2] != -1) return dp[st][x1][x2];

    int y1 = st - x1 + 2;
    int y2 = st - x2 + 2;

    dp[st][x1][x2] = 0;

    if (x1>n || x2>n || y1>m || y2>m) return dp[st][x1][x2];

    dp[st][x1][x2] = max (DFS(st+1, x1+1, x2), max(DFS(st+1, x1, x2+1), max(DFS(st+1, x1+1, x2+1), DFS(st+1, x1, x2))));

    if (x1 == x2)
        dp[st][x1][x2] += a[x1][y1];
    else dp[st][x1][x2] += a[x1][y1] + a[x2][y2];

    return dp[st][x1][x2];
}


int main ()
{
    int t, nCase = 1;
    scanf ("%d", &t);

    while (t--)
    {
        met (a, 0);
        met (dp, -1);
        scanf ("%d %d", &n, &m);

        for (int i=1; i<=n; i++)
        for (int j=1; j<=m; j++)
        scanf ("%d", &a[i][j]);

        printf ("Case %d: %d\n", nCase++, DFS (0, 1, 1));
    }
    return 0;
}


你可能感兴趣的:(light Oj 1071 - Baker Vai(记忆化搜索))