[LeetCode] 1368. Minimum Cost to Make at Least One Valid Path in a Grid 使網格圖至少有一條有效路徑的最小代價
Given an m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be:
1which means go to the cell to the right. (i.e go fromgrid[i][j]togrid[i][j + 1])2which means go to the cell to the left. (i.e go fromgrid[i][j]togrid[i][j - 1])3which means go to the lower cell. (i.e go fromgrid[i][j]togrid[i + 1][j])4which means go to the upper cell. (i.e go fromgrid[i][j]togrid[i - 1][j])
Notice that there could be some signs on the cells of the grid that point outside the grid.
You will initially start at the upper left cell (0, 0). A valid path in the grid is a path that starts from the upper left cell (0, 0) and ends at the bottom-right cell (m - 1, n - 1) following the signs on the grid. The valid path does not have to be the shortest.
You can modify the sign on a cell with cost = 1. You can modify the sign on a cell one time only.
Return the minimum cost to make the grid have at least one valid path.
Example 1:

Input: grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
Output: 3
Explanation: You will start at point (0, 0).
The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3)
The total cost = 3.
Example 2:

Input: grid = [[1,1,3],[3,2,2],[1,1,4]]
Output: 0
Explanation: You can follow the path from (0, 0) to (2, 2).
Example 3:

Input: grid = [[1,2],[4,3]]
Output: 1
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 1001 <= grid[i][j] <= 4
這道題說是給了一個 mxn 的二維數組,值為 1,2,3,4 中的數字,代表右,左,下,上,四個方向,說是如果按照指示的方向到下一個位置,就沒有 cost,否則會有1個 cost?,F在問以左上角為起點,走到右下角的終點最少需要多少個 cost。乍眼一看感覺像是個迷宮遍歷求最短路徑的問題,但是這道題求的卻不是能到達終點的最少步數,而是問最小 cost,那么步數的多少和 cost 的大小并沒有直接的聯系,比如例子2中是把數組中的所有位置走了個遍,才能得到 cost 為0的最優解。
將這里箭頭的方向看作是權重值為0的邊,其他方向則是權重為1的邊,則這里的數組就可以看作是有向權重圖,求的就是單源點的最短路徑了。當然在有向權重圖里的最短路徑就是權重和最小的路徑,正好就是本題中的 cost,完美契合。此時就要祭出神器迪杰斯特拉算法 Dijkstra Algorithm 了,LeetCode 中使用了該算法的題目還有 Network Delay Time 和 The Maze II。Dijkstra 算法是以起點為中心,向外層層擴展,直到擴展到終點為止。
根據這特性,用 BFS 來實現時再好不過了,這里要先引入松弛操作 Relaxtion,這是這個算法的核心思想,當有條路徑 (u, v) 是位置u到位置v,如果 cost(v) > cost(u) + w(u, v),那么 dist(v) 就可以被更新,這里的 cost(x) 就是從原點到達位置x的花費,w(u, v) 就是位置u和位置v的權重。在這道題中一般都是相鄰的兩個點進行比較,所以權重就是0或者1。這里使用 BFS 來進行遍歷,首先創建一個代表方向的二維數組,順序就按照題目中給定的順序右,左,下,上,這樣下標加1就是 grid 數組中的值了。然后還要建立一個二維數組 cost,這里 cost[i][j] 就表示從原點到達位置 (i, j) 的最小 cost,將 cost[0][0] 初始化為0,其他所有值初始化為 m + n - 2,這是因為就算每一步都增加1個花費,最多也就需要 m + n - 2 個花費就能到達終點。
接下來創建一個隊列 queue,里面放二維坐標 encode 成的一維坐標,比如二維坐標 (i, j) 就可以通過 i * n + j 變成一維坐標,初始化把0加入隊列中。接下來開始遍歷,當隊列不為空時進行循環,取出隊首元素,并且還原出二維坐標 (i, j)。此時判斷,若當前已經到達終點了,則用當前 cost[i][j] 更新結果 res,這里并不能直接返回 res,因為第一次到達終點時,并不能代表 cost 時最小的,所以是要遍歷整個數組的。然后對當前位置的右,左,下,上,四個方向進行遍歷,算出新的二維坐標,同時根據 grid 數組中的值算下要不要增加1個花費。之后進行判斷,假如新的位置越界了,直接跳過;或者當前算出的 curCost 大于 cost 數組中的值,則也跳過,這個實際就是松弛操作; 或者 curCost 大于結果 res 時,也跳過,通過這個剪枝可以提高運行效率。否則就用 curCost 來更新 cost 數組中對應的位置,并把當前位置加入隊列中,最終返回結果 res 即可,參見代碼如下:
class Solution {
public:
int minCost(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size(), res = m + n - 2;
vector<vector<int>> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<vector<int>> cost(m, vector<int>(n, m + n - 2));
cost[0][0] = 0;
queue<int> q{{0}};
while (!q.empty()) {
auto t = q.front(), i = t / n, j = t % n;
q.pop();
if (i == m - 1 && j == n - 1) {
res = min(res, cost[i][j]);
}
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k][0], y = j + dirs[k][1];
int curCost = grid[i][j] == k + 1 ? cost[i][j] : (cost[i][j] + 1);
if (x < 0 || x >= m || y < 0 || y >= n || curCost >= cost[x][y] || curCost >= res) continue;
cost[x][y] = curCost;
q.push(x * n + y);
}
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/1368
類似題目:
Minimum Weighted Subgraph With the Required Paths
Disconnect Path in a Binary Matrix by at Most One Flip
參考資料:
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/


浙公網安備 33010602011771號