dijistra
class Solution {
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
public:
int minCost(vector<vector<int>>& grid) {
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> >pq;
int m = grid.size(), n = grid[0].size();
vector<int>dist(m*n, INT_MAX/10);
vector<bool>visited(m*n, false);
pq.push(make_pair(0,0));
dist[0] = 0;
while(!pq.empty())
{
int curDist = pq.top().first;
int curPos = pq.top().second;
pq.pop();
int x = curPos / n, y = curPos % n;
if(visited[curPos] == true) continue;
visited[curPos] = true;
for(int i = 0; i < 4; i++)
{
int nextX = x + dx[i];
int nextY = y + dy[i];
int newPos = nextX * n + nextY;
int newDist = curDist + (grid[x][y] != i + 1);
if(nextX >= 0 && nextX < m && nextY >= 0 && nextY < n && newDist < dist[newPos])
{
dist[newPos] = newDist;
pq.push(make_pair(newDist, newPos));
}
}
}
return dist[m*n - 1];
}
};
bfs
class Solution {
public:
int minCost(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>>dist(m, vector<int>(n, INT_MAX/10));
queue<pair<int, int> >q;
q.push(make_pair(0, 0));
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
dist[0][0] = 0;
while(!q.empty())
{
int x = q.front().first, y = q.front().second;
q.pop();
for(int i = 0; i < 4; i++)
{
int nextX = x + dx[i], nextY = y + dy[i];
if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n)
{
continue;
}
int newDict = dist[x][y] + (grid[x][y]==i+1 ? 0 : 1);
if( newDict < dist[nextX][nextY])
{
dist[nextX][nextY] = newDict;
q.push(make_pair(nextX, nextY));
}
}
}
return dist[m-1][n-1];
}
};
SPFA
class Solution {
public:
int minCost(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>>dist(m, vector<int>(n, INT_MAX/10));
vector<vector<bool>>visited(m, vector<bool>(n, false));
queue<pair<int, int> >q;
q.push(make_pair(0, 0));
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
dist[0][0] = 0;
while(!q.empty())
{
int x = q.front().first, y = q.front().second;
q.pop();
visited[x][y] = false;
for(int i = 0; i < 4; i++)
{
int nextX = x + dx[i], nextY = y + dy[i];
if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n)
{
continue;
}
int newDict = dist[x][y] + (grid[x][y]==i+1 ? 0 : 1);
if( newDict < dist[nextX][nextY])
{
dist[nextX][nextY] = newDict;
if(visited[nextX][nextY] == false)
{
q.push(make_pair(nextX, nextY));
}
}
}
}
return dist[m-1][n-1];
}
};
0-1 BFS
class Solution {
public:
int minCost(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>>dist(m, vector<int>(n, INT_MAX/10));
deque<pair<int, int> >dq;
dq.push_front(make_pair(0, 0));
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
dist[0][0] = 0;
while(!dq.empty())
{
int x = dq.front().first, y = dq.front().second;
dq.pop_front();
for(int i = 0; i < 4; i++)
{
int nextX = x + dx[i], nextY = y + dy[i];
if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n)
{
continue;
}
int newDict = dist[x][y] + (grid[x][y]==i+1 ? 0 : 1);
if( newDict < dist[nextX][nextY])
{
dist[nextX][nextY] = newDict;
if(grid[x][y] == i + 1)
{
dq.push_front(make_pair(nextX, nextY));
}
else
{
dq.push_back(make_pair(nextX, nextY));
}
}
}
}
return dist[m-1][n-1];
}
};
参考:
https://leetcode-cn.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/solution/shi-wang-ge-tu-zhi-shao-you-yi-tiao-you-xiao-lu-2/
https://leetcode-cn.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/solution/zui-duan-lu-jing-suan-fa-bfs0-1bfsdijkstra-by-luci/