classSolution { public: typedefpair<int, int> PII; int res; int m; int n; vector<vector<int>> grid; vector<vector<int>> dp; /* void dfs(int x, int y, int sum) { if (x == m - 1 && y == n -1) { res = max(res, sum); return; } int dx[] = {0, 1}; int dy[] = {1, 0}; for (int k = 0; k < 2; ++k) { int nx = x + dx[k], ny = y + dy[k]; if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] >= 0) { int b = grid[nxa][ny]; grid[nx][ny] = -1; dfs(nx, ny, sum + b); grid[nx][ny] = b; } } } int getMaxValue(vector<vector<int>>& _grid) { grid = _grid; m = grid.size(); n = grid[0].size(); dfs(0, 0, grid[0][0]); return res; } */
intdfs(int i, int j){ if (dp[i][i] >= 0) { return dp[i][j]; } if (i == 0 && j == 0) { return dp[i][j] = grid[i][j]; } dp[i][j] = grid[i][j] + max( j - 1 >= 0 ? dfs(i, j - 1) : INT_MIN, i - 1 >= 0 ? dfs(i - 1, j) : INT_MIN); return dp[i][j]; } intgetMaxValue(vector<vector<int>>& _grid){ grid = _grid; m = grid.size(); n = grid[0].size(); dp = vector<vector<int>>(m, vector<int>(n, -1)); dfs(m - 1, n - 1); return dp[m - 1][n - 1]; } };