当前位置:网站首页>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++
边栏推荐
- Sort() sort function
- 20211108 observable, controllable, stable and measurable
- LeetCode 6095. 强密码检验器 II
- LeetCode 6096. 咒语和药水的成功对数(二分查找)
- 20211006 积分、微分、投影均属于线性变换
- 20220524 how to install coppeliasim to disk D
- 攻防世界-PWN-shell
- 20211006 integral, differential and projection belong to linear transformation
- Lecture par lots de tous les fichiers vocaux sous le dossier
- 【最全面详细解释】背包问题详解
猜你喜欢

JUC atomic reference and ABA problem

BGP 联邦+Community

Solov2 source code analysis

C language: Simulated Implementation of library function strcpy

C language: recursive function to realize Hanoi Tower

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

C language: deep understanding of pointers and arrays

C/S模型与P2P模型
![[implementation of depth first search]](/img/10/4f150e4fa0d4edf01483a72b881afe.jpg)
[implementation of depth first search]
Drill down to protobuf - Introduction
随机推荐
静态变量与一个类相关联,只要该类在内存中(只要您的应用程序终止,该变量就不存在)就可以使用。(堆本体,栈引用)
Use typescript to complete simple snake eating function
Yolov5 model evaluation
C language: recursive function to realize Hanoi Tower
C language: deep understanding of character functions and string functions (2)
20211018 一些特殊矩阵
SQL ROW_ The number() function uses
20211006 线性变换
Talking about acid of database
Two good kids
HAProxy + Keepalived实现MySQL的高可用负载均衡
20211108 differential tracker
Timestamp to localdate
C language: summary of question brushing (1)
20211018 some special matrices
Redis fuzzy query batch deletion
Summary of the first retrospective meeting
Online debugging tool Arthas Foundation
批量讀取文件夾下的全部語音文件
类的加载概述