您的位置:首页 > 大数据 > 人工智能

Light oj 1071 - Baker Vai(记忆化)

2015-09-15 15:03 495 查看
1071 - Baker Vai



PDF (English)StatisticsForum
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]

;
int n,m;
int a

;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: