#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;
}
递归版本