lightoj 1071 Baker Vai (思维dp)

177 篇文章 0 订阅
77 篇文章 8 订阅
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

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
    
Output for Sample Input
Case 1: 8
Case 2: 18

题目链接:http://lightoj.com/volume_showproblem.php?problem=1071

题目大意:一个n*m的矩阵一个人从左上走到右下,再从右下回到左上,每个格子有数字,同一个格子只能走一次除了起点和终点但是数字只能算一次,求得到的数字和最大是多少

题目分析:其实整个过程可以当做有两个人同时从左上走到右下,中间除了起点和终点,两人不能处在同一个格子上,求到终点时两人数字和的最大值,设dp[i][j][k]为两人分别走了k步时第一个人向右走了i步,第二个人向右走了j步的最大值,知道了一共走了几步,又知道向右走了几步,那么向下走了几步是可以算出来的,设总共走了sum步,向右y步,则向下走的步数为sum + 2 - y,注意起点和终点的值只能算一次,状态转移的时候总共四种情况,A下B右,A下B下,A右B下,A右B右,最后的答案就是dp[m][m][n + m - 2]

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 105;
int a[MAX][MAX];
int dp[MAX][MAX][MAX << 1];
int n, m;

int main() {
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca ++) {
        printf("Case %d: ", ca);
        memset(dp, 0, sizeof(dp));
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; i ++) {
            for(int j = 1; j <= m; j ++) {
                scanf("%d", &a[i][j]);
            }
        }
        int x1, y1, x2, y2, val;
        dp[1][1][0] = a[1][1];
        for(int k = 1; k <= n + m - 2; k ++) {
            for(int i = 1; i <= m; i ++) {
                for(int j = 1; j <= m; j ++) {
                    y1 = i;
                    x1 = k + 2 - y1;
                    y2 = j;
                    x2 = k + 2 - y2;
                    if(x1 == 1 && y1 == 1 && x2 == 1 && y2 == 1) {
                        continue;
                    } 
                    if(x1 > n || x2 > n || x1 < 1 || x2 < 1) {
                        continue;
                    }
                    if(x1 == x2 && y1 == y2 && !(x1 == n && y1 == m && x2 == n && y2 == m)) {
                        continue;
                    }
                    if(x1 == n && y1 == m && x2 == n && y2 == m) {
                        val = a[x1][y2];
                    }
                    else {
                        val = a[x1][y1] + a[x2][y2];
                    }
                    dp[y1][y2][k] = max(dp[y1][y2][k], dp[y1 - 1][y2][k - 1] + val);
                    dp[y1][y2][k] = max(dp[y1][y2][k], dp[y1][y2 - 1][k - 1] + val);
                    dp[y1][y2][k] = max(dp[y1][y2][k], dp[y1][y2][k - 1] + val);
                    dp[y1][y2][k] = max(dp[y1][y2][k], dp[y1 - 1][y2 - 1][k - 1] + val);
                }
            }
        }
        printf("%d\n", dp[m][m][m + n - 2]);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值