cpp-algo-cases/chapter_dynamic_programming/min_path_sum.cpp

117 lines
3.3 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: min_path_sum.cpp
* Created Time: 2023-07-10
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><>С·<D0A1><C2B7><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int minPathSumDFS(vector<vector<int>> &grid, int i, int j) {
// <20><>Ϊ<EFBFBD><CEAA><EFBFBD>Ͻǵ<CFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
if (i == 0 && j == 0) {
return grid[0][0];
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>򷵻<EFBFBD> +<2B><> <20><><EFBFBD><EFBFBD>
if (i < 0 || j < 0) {
return INT_MAX;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<CFBD> (i-1, j) <20><> (i, j-1) <20><><EFBFBD><EFBFBD>С·<D0A1><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int up = minPathSumDFS(grid, i - 1, j);
int left = minPathSumDFS(grid, i, j - 1);
// <20><><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD>Ͻǵ<CFBD> (i, j) <20><><EFBFBD><EFBFBD>С·<D0A1><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
}
/* <20><>С·<D0A1><C2B7><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><E4BBAF><EFBFBD><EFBFBD> */
int minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {
// <20><>Ϊ<EFBFBD><CEAA><EFBFBD>Ͻǵ<CFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
if (i == 0 && j == 0) {
return grid[0][0];
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>򷵻<EFBFBD> +<2B><> <20><><EFBFBD><EFBFBD>
if (i < 0 || j < 0) {
return INT_MAX;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
if (mem[i][j] != -1) {
return mem[i][j];
}
// <20><><EFBFBD>ߺ<EFBFBD><DFBA>ϱߵ<CFB1>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С·<D0A1><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int up = minPathSumDFSMem(grid, mem, i - 1, j);
int left = minPathSumDFSMem(grid, mem, i, j - 1);
// <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<CFBD> (i, j) <20><><EFBFBD><EFBFBD>С·<D0A1><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
return mem[i][j];
}
/* <20><>С·<D0A1><C2B7><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD>̬<EFBFBD>滮 */
int minPathSumDP(vector<vector<int>> &grid) {
int n = grid.size(), m = grid[0].size();
// <20><>ʼ<EFBFBD><CABC> dp <20><>
vector<vector<int>> dp(n, vector<int>(m));
dp[0][0] = grid[0][0];
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>
for (int j = 1; j < m; j++) {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD><D0BA><EFBFBD>
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];
}
}
return dp[n - 1][m - 1];
}
/* <20><>С·<D0A1><C2B7><EFBFBD>ͣ<EFBFBD><CDA3>ռ<EFBFBD><D5BC>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD>Ķ<EFBFBD>̬<EFBFBD>滮 */
int minPathSumDPComp(vector<vector<int>> &grid) {
int n = grid.size(), m = grid[0].size();
// <20><>ʼ<EFBFBD><CABC> dp <20><>
vector<int> dp(m);
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>
dp[0] = grid[0][0];
for (int j = 1; j < m; j++) {
dp[j] = dp[j - 1] + grid[0][j];
}
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 1; i < n; i++) {
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>
dp[0] = dp[0] + grid[i][0];
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int j = 1; j < m; j++) {
dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];
}
}
return dp[m - 1];
}
/* Driver Code */
int main() {
vector<vector<int>> grid = {{1, 3, 1, 5}, {2, 2, 4, 2}, {5, 3, 2, 1}, {4, 3, 5, 2}};
int n = grid.size(), m = grid[0].size();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int res = minPathSumDFS(grid, n - 1, m - 1);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ǵ<EFBFBD><EFBFBD><EFBFBD>С·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << res << endl;
// <20><><EFBFBD><EFBFBD><E4BBAF><EFBFBD><EFBFBD>
vector<vector<int>> mem(n, vector<int>(m, -1));
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ǵ<EFBFBD><EFBFBD><EFBFBD>С·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << res << endl;
// <20><>̬<EFBFBD>
res = minPathSumDP(grid);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ǵ<EFBFBD><EFBFBD><EFBFBD>С·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << res << endl;
// <20>ռ<EFBFBD><D5BC>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD>Ķ<EFBFBD>̬<EFBFBD>
res = minPathSumDPComp(grid);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ǵ<EFBFBD><EFBFBD><EFBFBD>С·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << res << endl;
return 0;
}