Light oj 1071 - Baker Vai(记忆化)

1071 - Baker Vai
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

 


PROBLEM SETTER: JANE ALAM JAN


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-8
typedef long long ll;

using namespace std;

#define INF 0x3f3f3f3f
#define N 105

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

int dfs(int step,int x,int xx)
{
    if(x==xx&&x!=0&&x!=n-1) return -INF;
    if(step==n+m-2)
    {
        if(x==xx&&x==n-1)
            return a[n-1][m-1];
        return -INF;
    }
    if(dp[step][x][xx]>=0) return dp[step][x][xx];
    int ans=-INF;

    if(x+1<n&&xx+1<n) ans=max(ans,dfs(step+1,x+1,xx+1));
    if(x+1<n&&step-xx+1<m) ans=max(ans,dfs(step+1,x+1,xx));
    if(step-x+1<m&&xx+1<n) ans=max(ans,dfs(step+1,x,xx+1));
    if(step-x+1<m&&step-xx+1<m) ans=max(ans,dfs(step+1,x,xx));
    ans+=a[x][step-x];
    if(x!=xx) ans+=a[xx][step-xx];
    return dp[step][x][xx]=ans;
}

int main()
{
    int i,j,t,ca=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
               scanf("%d",&a[i][j]);
        memset(dp,-1,sizeof(dp));
        printf("Case %d: %d\n",++ca,dfs(0,0,0));
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值