---
# System prepended metadata

title: 2304. Minimum Path Cost in a Grid
tags: [Leetcode, Dynamic Programming, Medium]

---

# 2304. Minimum Path Cost in a Grid
###### tags: `Leetcode` `Medium` `Dynamic Programming`
Link: https://leetcode.com/problems/minimum-path-cost-in-a-grid/description/
## 思路
迷宫型dp
## Code
```java=
class Solution {
    public int minPathCost(int[][] grid, int[][] moveCost) {
        int m = grid.length, n = grid[0].length;
        int[] dp = grid[0];
        for(int i=1; i<m; i++){
            int[] tmp = new int[n];
            Arrays.fill(tmp, Integer.MAX_VALUE);
            for(int j=0; j<n; j++){
                for(int k=0; k<n; k++){
                    tmp[j] = Math.min(tmp[j], dp[k]+moveCost[grid[i-1][k]][j]);
                }
            }
            for(int j=0; j<n; j++){
                tmp[j] += grid[i][j];
            }
            dp = tmp;
        }
        int ans = Integer.MAX_VALUE;
        for(int i=0; i<n; i++) ans = Math.min(ans, dp[i]);
        return ans;
    }
}
```