lightoj 1071 - Baker Vai(双线DP)

来源:互联网 发布:用java做幸运抽奖 编辑:程序博客网 时间:2024/05/16 14:48

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

 

这题的大意是让你从矩阵的左上角走到右下角,然后再从右下角走到左上角,两次不走重复的点,走过总和的最大值。
思路:可以把两次看做是从左上角到右下角两条线一起走,用三维数组记录第一条线的横坐标,和第二条线的横坐标还有走过的距离,这样就可以算出纵坐标。
这种题目以前在NYOJ上做过一次,可能是数据太水了吧,以前的方法在这题上就过不去了。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int dp[105][105][210];int a[105][105];int main(void){    int T,n,m,i,j;    scanf("%d",&T);    int cas = 1;    while(T--)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)            for(j=1;j<=m;j++)                scanf("%d",&a[i][j]);        int x1,x2,y1,y2,k;        memset(dp,-1,sizeof(dp));        dp[2][1][1]=a[1][1]+a[2][1]+a[1][2];        for(k=1;k<m+n-3;k++)            for(x1=1;x1<=n;x1++)                for(x2=1;x2<=n;x2++)                {                    if(dp[x1][x2][k] == -1)                        continue;                    y1 = k - x1 + 2;                    y2 = k - x2 + 2;                    if(x1 < n)                        dp[x1+1][x2+1][k+1]=max(dp[x1+1][x2+1][k+1],dp[x1][x2][k]+a[x1+1][y1]+a[x2+1][y2]);                    if(y2 < m)                        dp[x1][x2][k+1]=max(dp[x1][x2][k+1],dp[x1][x2][k]+a[x1][y1+1]+a[x2][y2+1]);                    if(x1 < n && y2 < m)                        dp[x1+1][x2][k+1]=max(dp[x1+1][x2][k+1],dp[x1][x2][k]+a[x1+1][y1]+a[x2][y2+1]);                    if(x2+1 < x1)                        dp[x1][x2+1][k+1]=max(dp[x1][x2+1][k+1],dp[x1][x2][k]+a[x1][y1+1]+a[x2+1][y2]);                }        printf("Case %d: %d\n",cas++,dp[n][n-1][n+m-3]+a[n][m]);    }    return 0;}


0 0
原创粉丝点击