题目描述
在一片nm的土地上,每一块11的区域里都有一定数量的金子。这一天,你到这里来淘金,然而当地人告诉你,如果你挖了某一区域的金子,上一行,下一行,左边,右边的金子你都不能被允许挖了。那么问题来了:你最多能淘金多少?
输入
对于每组数据,第一行两个数n,m,表示土地的长和宽(1<=n,m<=200)
接下来n行,每行m个数,表示每个区域的金子数量,每个区域的金子数量不超过1000
输出
对于每组数据,输出最多得到的金子数量
样例输入
4 6
11 0 7 5 13 9
78 4 81 6 22 4
1 40 9 34 16 10
11 22 0 33 39 6
样例输出
242
思路:将这个数组的每一行先求出最大值,然后从所有的行最大值里再求出一个总的最大值,类似于pipi的地头蛇,只是里面嵌套一个一模一样方法
#include <bits/stdc++.h>
using namespace std;
int n,m;
int mp[205][205];
int b[205];
int getmaxglod(int row)
{
int last_do=0,last_undo=0,now_do=0,now_undo=0;
for(int i=1; i<=m; i++)
{
now_do=last_undo+mp[row][i];
now_undo=max(last_do,last_undo);
last_do=now_do;
last_undo=now_undo;
}
return max(now_do,now_undo);
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&mp[i][j]);
for(int i=1; i<=n; i++)
b[i]=getmaxglod(i);
int last_do=0,last_undo=0,now_do=0,now_undo=0;
for(int i=1; i<=n; i++)
{
now_do=last_undo+b[i];
now_undo=max(last_do,last_undo);
last_do=now_do;
last_undo=now_undo;
}
printf("%d\n",max(now_do,now_undo));
}
return 0;
}