#include<iostream>
#include<vector>
using namespace std;
class Solution {
int maxnow = 0;
int maxfinal = 0;//一定超时
void dfs(vector<vector<int>>& grid, int st_i, int st_j)
{
if (st_i == grid.size()-1 && st_j == grid[grid.size() - 1].size()-1)
{
if (maxnow > maxfinal)
maxfinal = maxnow;
return;
}
else
{
if (st_i + 1 < grid.size())
{
maxnow = maxnow + grid[st_i + 1][st_j];
dfs(grid, st_i + 1, st_j);
maxnow = maxnow - grid[st_i + 1][st_j];
}
if (st_j + 1 < grid[grid.size() - 1].size())
{
maxnow = maxnow + grid[st_i][st_j + 1];
dfs(grid, st_i, st_j + 1);
maxnow = maxnow - grid[st_i][st_j + 1];
}
}
}
public:
int maxValue(vector<vector<int>>& grid) {
if (grid.size() != 0)
{
maxnow += grid[0][0];
}
dfs(grid, 0, 0);
return maxfinal;
}
};
int main()
{
vector<vector<int>>grid = {
{5,0,1,1,2,1,0,1,3,6,3,0,7,3,3,3,1},
{1,4,1,8,5,5,5,6,8,7,0,4,3,9,9,6,0},
{2,8,3,3,1,6,1,4,9,0,9,2,3,3,3,8,4},
{3,5,1,9,3,0,8,3,4,3,4,6,9,6,8,9,9},
{3,0,7,4,6,6,4,6,8,8,9,3,8,3,9,3,4},
{8,8,6,8,3,3,1,7,9,3,3,9,2,4,3,5,1},
{7,1,0,4,7,8,4,6,4,2,1,3,7,8,3,5,4},
{3,0,9,6,7,8,9,2,0,4,6,3,9,7,2,0,7},
{8,0,8,2,6,4,4,0,9,3,8,4,0,4,7,0,4},
{3,7,4,5,9,4,9,7,9,8,7,4,0,4,2,0,4},
{5,9,0,1,9,1,5,9,5,5,3,4,6,9,8,5,6},
{5,7,2,4,4,4,2,1,8,4,8,0,5,4,7,4,7},
{9,5,8,6,4,4,3,9,8,1,1,8,7,7,3,6,9},
{7,2,3,1,6,3,6,6,6,3,2,3,9,9,4,4,8}
};
Solution a;
cout<<a.maxValue(grid);
}
#include<iostream>
#include<vector>
using namespace std;
// class Solution {
// int map[202][202]={0};
// public:
// int maxValue(vector<vector<int>>& grid) {
// for(int i=grid.size();i>=0;i--)
// {
// for(int j=grid[0].size();j>=0;j--)
// {
// if(i==grid.size()||j==grid[0].size())
// {
// map[i][j]=0;
// }
// else
// {
// map[i][j]=max(map[i+1][j],map[i][j+1])+grid[i][j];
// }
// }
// }
// return map[0][0];
// }
// };
class Solution {//map也不必要 直接可以在grid 上面改
public:
int maxValue(vector<vector<int>>& grid) {
for(int i=grid.size()-1;i>=0;i--)
{
for(int j=grid[0].size()-1;j>=0;j--)
{
if(i==grid.size()-1&&j!=grid[0].size()-1)
{
grid[i][j]=grid[i][j+1]+grid[i][j];
}
else if(i!=grid.size()-1&&j==grid[0].size()-1)
{
grid[i][j]=grid[i+1][j]+grid[i][j];
}
else if(i==grid.size()-1&&j==grid[0].size()-1)
{
grid[i][j]=grid[i][j];
}
else
{
grid[i][j]=max(grid[i+1][j],grid[i][j+1])+grid[i][j];
}
}
}
return grid[0][0];
}
};
#include<iostream>
#include<vector>
using namespace std;
class Solution {
//递归的动态规划记录表法 自顶向下
int map[202][202]={0};//用来储存从 i j位置到终点位置所需要的最大价值
int dfs(vector<vector<int>>& grid, int st_i, int st_j)
{
if(map[st_i][st_j]!=0)//如果记录表有元素了 那直接返回 不要多余搜索
{
return map[st_i][st_j];
}
if(st_i>=grid.size()||st_j>=grid[0].size())return 0;//如果过边界了 那说明不存在这种情况
//所获得的礼物价值一定是0
else
{//第 i j位置的值与 i+1 j i j+1的值有关
map[st_i][st_j]=max(dfs(grid,st_i+1,st_j),dfs(grid,st_i,st_j+1))+grid[st_i][st_j];
return map[st_i][st_j];
}
}
public:
int maxValue(vector<vector<int>>& grid) {
return dfs(grid, 0, 0);
}
};
int main()
{
vector<vector<int>>grid = {
{5,0,1,1,2,1,0,1,3,6,3,0,7,3,3,3,1},
{1,4,1,8,5,5,5,6,8,7,0,4,3,9,9,6,0},
{2,8,3,3,1,6,1,4,9,0,9,2,3,3,3,8,4},
{3,5,1,9,3,0,8,3,4,3,4,6,9,6,8,9,9},
{3,0,7,4,6,6,4,6,8,8,9,3,8,3,9,3,4},
{8,8,6,8,3,3,1,7,9,3,3,9,2,4,3,5,1},
{7,1,0,4,7,8,4,6,4,2,1,3,7,8,3,5,4},
{3,0,9,6,7,8,9,2,0,4,6,3,9,7,2,0,7},
{8,0,8,2,6,4,4,0,9,3,8,4,0,4,7,0,4},
{3,7,4,5,9,4,9,7,9,8,7,4,0,4,2,0,4},
{5,9,0,1,9,1,5,9,5,5,3,4,6,9,8,5,6},
{5,7,2,4,4,4,2,1,8,4,8,0,5,4,7,4,7},
{9,5,8,6,4,4,3,9,8,1,1,8,7,7,3,6,9},
{7,2,3,1,6,3,6,6,6,3,2,3,9,9,4,4,8}
};
Solution a;
cout<<a.maxValue(grid);
}