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
#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;}
- lightoj 1071 - Baker Vai(双线DP)
- lightoj 1071 - Baker Vai DP
- LightOJ 1071 - Baker Vai(水DP)
- lightoj 1071 Baker Vai (思维dp)
- LightOj--1071--Baker Vai(拆点最大费用流)
- LightOJ - 1071 Baker Vai(记忆化搜索)
- LightOJ 1071 - Baker Vai 最大费用流
- Light OJ 1071 Baker Vai (DP)
- lightoj 1071 - Baker Vai 【最大费用最大流】
- Baker Vai Light OJ 1071
- Light oj 1071 - Baker Vai(记忆化)
- light Oj 1071 - Baker Vai(记忆化搜索)
- Lightoj1071:Baker Vai(最小费用最大流)
- LightOJ-1071-dp,dfs
- hdu 2686 Matrix(双线程DP)
- 2014省赛大总结(三) 双线DP
- 蓝桥杯 - 传纸条 (双线程DP)
- nyoj 61(双线程dp)
- HttpClient发起post、get请求
- 利用Oracle动态游标实现动态SQL循环遍历
- js Date
- Android性能优化篇:Android中如何避免创建不必要的对象
- Java中的Socket编程
- lightoj 1071 - Baker Vai(双线DP)
- html 柱状图
- Ubuntu 14.04常用软件及其安装
- JSP学习笔记-第一课时
- Could not initialize class com.taobao.diamond.client.impl.DiamondEnvRepo
- 关于投资的监控体系
- [HAOI2007]反素数
- android studio 一直卡在Gradle:Build Running的解决办法
- 为什么重写equals一定要重写hashCode