当前位置:网站首页>LeetCode 5270. Minimum path cost in grid (dynamic programming)
LeetCode 5270. Minimum path cost in grid (dynamic programming)
2022-06-13 09:18:00 【Michael Amin】
List of articles
1. subject
I'll give you a subscript from 0 The starting integer matrix grid , The matrix size is m x n , From 0 To m * n - 1 Is composed of different integers . You can use this matrix , Move from a cell to The next line Any other cell of the . If you are in a cell (x, y) , And meet x < m - 1 , You can move to (x + 1, 0), (x + 1, 1), ..., (x + 1, n - 1) Any cell in the . Be careful : Cells in the last row cannot trigger a move .
Every possible move comes at a corresponding cost , The price is from with a subscript 0 Start with a two-dimensional array moveCost Express , The array size is (m * n) x n , among moveCost[i][j] It's from The value is i The cell of moves to the next row j Column The cost of cells . from grid The cost of cell movement in the last row can be ignored .
grid The cost of a path is : Of all cells the path passes through Sum of values add All mobile Sum of costs . from first line Start from any cell , Return to The last line Of any cell Minimum Path cost .
Example 1:
Input :grid = [[5,3],[4,0],[2,1]],
moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]
Output :17
explain : The path with the least cost is 5 -> 0 -> 1 .
- The path passes through the sum of cell values 5 + 0 + 1 = 6 .
- from 5 Move to 0 The price is 3 .
- from 0 Move to 1 The price is 8 .
The total cost of the path is 6 + 3 + 8 = 17 .
Example 2:
Input :grid = [[5,1,2],[4,0,3]],
moveCost = [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]]
Output :6
explain :
The path with the least cost is 2 -> 3 .
- The path passes through the sum of cell values 2 + 3 = 5 .
- from 2 Move to 3 The price is 1 .
The total cost of the path is 5 + 1 = 6 .
Tips :
m == grid.length
n == grid[i].length
2 <= m, n <= 50
grid From 0 To m * n - 1 Is composed of different integers
moveCost.length == m * n
moveCost[i].length == n
1 <= moveCost[i][j] <= 100source : Power button (LeetCode) link :https://leetcode.cn/problems/minimum-path-cost-in-a-grid Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Problem solving
dp[i][j]Indicates arrival(i, j)The minimum cost of time
class Solution {
public:
int minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> dp(m, vector<int>(n, INT_MAX));
for(int j = 0; j < n; ++j)
dp[0][j] = grid[0][j];
for(int i = 1; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
for(int k = 0; k < n; ++k)
{
dp[i][j] = min(dp[i][j], dp[i-1][k]+moveCost[grid[i-1][k]][j]+grid[i][j]);
}
}
}
return *min_element(dp.back().begin(), dp.back().end());
}
};272 ms 78.5 MB C++
边栏推荐
- C/s model and P2P model
- 20220524 how to install coppeliasim to disk D
- C language: preprocessing in program environment
- 20211006 线性变换
- LeetCode 1143. 最长公共子序列
- Some websites of QT (software download, help documents, etc.)
- 攻防世界-PWN-shell
- C language: deep understanding of character functions and string functions (1)
- Routing - static routing
- 20211005 Hermite矩阵及几个性质
猜你喜欢

turtle库显示系统时间

类的加载概述

Simulink variant model and variant subsystem usage

C language: five custom types

Storage mode of drawings

IP address introduction

Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing

全新BMW i3的操控,能符合对3系之名产品的期待吗?

20220606 Young's inequality for Matrices

20220524 如何把CoppeliaSim安装到D盘
随机推荐
Exporting MySQL data table documents using Navicat
静态变量与一个类相关联,只要该类在内存中(只要您的应用程序终止,该变量就不存在)就可以使用。(堆本体,栈引用)
Yolov5 face video stream
20211108 det(AB)=det(A)det(B)
20211104 为什么相似矩阵的迹相同
QT multithreaded TCP server
Tutorial (5.0) 03 Security policy * fortiedr * Fortinet network security expert NSE 5
Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
Routing - static routing
""? "& in URL Role of "" sign
Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
20211006 integral, differential and projection belong to linear transformation
LeetCode 6097. 替换字符后匹配(字典)
20211108 observable, controllable, stable and measurable
20211108 is transpose multiply a a a positive definite matrix? What are the necessary and sufficient conditions for a to be a positive definite matrix?
Necessary and sufficient conditions for diagonalization of 20211115 matrix; The full rank matrix does not necessarily have n linearly independent eigenvectors; Symmetric matrices must be diagonalized
20220606 关于矩阵的Young不等式
C language: timer principle
虚拟化和云计算文章大合集
C/S模型与P2P模型