-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1368.minimum-cost-to-make-at-least-one-valid-path-in-a-grid.cpp
61 lines (44 loc) · 1.49 KB
/
1368.minimum-cost-to-make-at-least-one-valid-path-in-a-grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* @lc app=leetcode id=1368 lang=cpp
*
* [1368] Minimum Cost to Make at Least One Valid Path in a Grid
*/
// @lc code=start
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> dirs = {{0,1},{0,-1},{1,0},{-1,0}};
class Solution {
public:
int minCost(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> dist(m, vector<int>(n, INT_MAX));
deque<pair<int,int>> dq;
dist[0][0] = 0;
dq.push_back({0,0});
while (!dq.empty()) {
auto [r, c] = dq.front();
dq.pop_front();
int currCost = dist[r][c];
for (int i = 0; i < 4; i++) {
int nr = r + dirs[i].first;
int nc = c + dirs[i].second;
if (nr < 0 || nr >= m || nc < 0 || nc >= n) {
continue;
}
int extra = (grid[r][c] == (i + 1)) ? 0 : 1;
int newCost = currCost + extra;
if (newCost < dist[nr][nc]) {
dist[nr][nc] = newCost;
if (extra == 0) {
dq.push_front({nr, nc});
} else {
dq.push_back({nr, nc});
}
}
}
}
return dist[m-1][n-1];
}
};
// @lc code=end