47.礼物的最大值

#include<iostream>
#include<vector>
using namespace std;

int maxOfGift(int*arry, int rows, int cols, int row, int col);
int maxGift(int*arry, int rows, int cols);
int maxOfGift(int*arry, int rows, int cols,int row,int col)
{
	if (row == rows -1 && col == cols - 1) return arry[(rows-1)*(cols-1)+cols-1]+arry[0];
	if (row == rows - 1)return  arry[row*cols+ col + 1]+maxOfGift(arry, rows, cols, row, col + 1);
	if (col == cols - 1)return  arry[(row + 1 )*cols+ col ] + maxOfGift(arry,  rows, cols, row + 1, col);
	return  (maxOfGift(arry,  rows, cols, row + 1, col) >= maxOfGift(arry,  rows, cols, row, col + 1) ? arry[(row + 1)*cols + col]+maxOfGift(arry, rows, cols, row + 1, col) : arry[row*cols + col + 1]+maxOfGift(arry, rows, cols, row, col + 1));
}
int  maxGift(int*arry, int rows, int cols)
{
	int a = maxOfGift(arry, rows, cols, 0, 0);
	return a;
}
void main()
{
	int *val = new int[16];
	val[0] = 1;val[1] = 10;
	val[2] = 3;val[3] = 8;
	val[4] = 12;val[5] = 2;
	val[6] = 9;val[7] = 6;
	val[8] = 5;val[9] = 7;
	val[10] = 4;val[11] = 11;
	val[12] = 3;val[13] = 7;
	val[14] = 16;val[15] = 5;

	cout << maxGift(val, 4, 4) << endl;
}

递归版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值