Light oj 1071 - Baker Vai(记忆化)
2015-09-15 15:03
495 查看
1071 - Baker Vai
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.
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.
PROBLEM SETTER: JANE ALAM JAN
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
#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; }
相关文章推荐
- codeforces #576E Painting Edges 分治+并查集
- failed to update auto layout status: the agent crashed within Xcode7
- API介绍:iBeacons、Sprite Kit、Game Center以及AirDrop等
- 用<%# DataBinder.Eval(Container.DataItem,"ID")%>显示数据的,标题太长规定字数,多余的用"..."
- Linux(C/C++)下的文件操作open、fopen与freopen http://blog.csdn.net/a656343072/article/details/40539889
- 实用的 AIX UNIX 命令
- IBM AIX 压缩
- AIX常用配置文件总结
- AIX 用SHELL脚本 自动FTP下载文件
- Linux wait()函数 http://blog.163.com/lqy_super/blog/static/1997510212012112953858902/
- 关于OC中Copy和retain的心得
- hdoj 1702 ACboy needs your help again!
- sql:[dbo].[smt_MES_RptProductDaily] 生产日报表
- 将信息写入keychain中
- clang: error: linker command failed with exit code 1 (use -v to see invocation)
- Traits的用法
- Traits的用法
- fcntl的close-on-exec标志 http://blog.csdn.net/sunlylorn/article/details/6363727
- [LeetCode]:217:Contains Duplicate
- LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt