在一个m * n的棋盘的每一个格都放有一个礼物,每个礼物都有一定价值(大于0)。从左上角开始拿礼物,每次向右或向下移动一格,直到右下角结束。给定一个棋盘,求拿到礼物的最大价值。例如,对于如下棋盘
1 10 3 8
12 2 9 6
5 7 4 11
3 7 16 5
- 礼物的最大价值为1+12+5+7+7+16+5=53。
int getMaxValue_solution2(vector<int> mat, int row, int col)
{
int result = -1;
if(mat.empty() || row < 1 || col < 1)
{
return result;
}
int* temp = new int[col];
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < col; ++j)
{
int up = 0;
int left = 0;
if(i > 0)
up = temp[j];
if(j > 0)
left = temp[j-1];
temp[j] = max(left , up) + mat[i * col + j];
}
}
result = temp[col - 1];
delete[] temp;
return result;
}