/ * Title: Given a chessboard m * n, each cell put a gift (values greater than 0 for each gift), starting from the upper left corner, lower right corner reaches down or right away, and the obtained presents the maximum. * / / * Idea: F (I, J) = max [F (. 1-I, J), F (I, J-. 1)] + A [I, J] * / #include <the iostream> #include < CString> #include <Vector> #include <algorithm> the using namespace STD; int getMaxValue (values const int *, int rows, int cols) { IF (rows <cols = 0 || <= 0) return 0; int * maxValues = new new int [cols]; maxValues [0] = values [0]; for (int COL =. 1; COL <cols; COL ++) { maxValues [COL] = maxValues [COL-. 1] + values [COL]; } for (int row = 1; row < rows; row ++) { for (COL int = 0; COL <cols; COL ++) { IF (COL == 0) { maxValues[0] = maxValues[0] + values[row * cols + col]; }else{ maxValues[col] = max(maxValues[col-1],maxValues[col]) + values[row * cols + col]; } } } return maxValues[cols - 1]; } int main(){ int values[]={1,10,3,8,12,2,9,6,5,7,4,11,3,7,16,5}; cout<<getMaxValue(values,4,4); return 0; }