class Solution {
public:
int shortestPath(vector<vector<int>>& grid, int k) {
vector<vector<int>> dir = {{1,0},{0,1},{0,-1},{-1,0}};
int m = grid.size(), n = grid[0].size(), i, j, ni, nj, s = 0, d;
vector<vector<vector<int>>> dp(m,vector<vector<int>>(n, vector<int>(k+1, INT_MAX)));
// dp[i][j][s] 表示到(i,j)位置,消除了s个障碍物的最短步数
dp[0][0][0] = 0;
vector<vector<bool>> vis(m, vector<bool>(n,false));
queue<vector<int>> q;
q.push({0,0});
vis[0][0] = true;
while (!q.empty()) //广度优先查找所有0位置,不用搬走障碍
int size = q.size();
while(size--)
i = q.front()[0];
j = q.front()[1];
q.pop();
dp[i][j][0] = s;//不用搬走障碍的最短步数
for(d = 0; d < 4; ++d)
ni = i+dir[d][0];
nj = j+dir[d][1];
if(ni<0 || ni >= m || nj<0 || nj >= n || vis[ni][nj] || grid[ni][nj])
continue;//出界了,访问过,是障碍物
q.push({ni,nj});
vis[ni][nj] = true;
for(s = 0; s <= k; s++)
{ //障碍物
for(i = 0; i < m; i++)
{ //位置 i
for(j = 0; j < n; j++)
{ //位置 j
if(dp[i][j][s] == INT_MAX)
continue;//状态不能走到,下一个
for(d= 0; d < 4; ++d)
{ //向4个方向移动,下一个坐标
ni = i+dir[d][0];
nj = j+dir[d][1];
if(ni<0 || ni >= m || nj<0 || nj >= n)
continue;//出界了,下一个
if(grid[ni][nj] && s+1 <= k)//下一个位置为障碍物, 还可以搬走
dp[ni][nj][s+1] = min(dp[ni][nj][s+1], dp[i][j][s]+1);
else if(!grid[ni][nj])//不是障碍物
dp[ni][nj][s] = min(dp[ni][nj][s], dp[i][j][s]+1);
int minstep = INT_MAX;
for(s = 0; s <= k; ++s)
minstep = min(minstep, dp[m-1][n-1][s]);
return minstep==INT_MAX ? -1 : minstep;
};
640 ms 25.2 MB
或者直接BFS,队列内存储
<i, j, 搬运障碍物次数>
class Solution {
public:
int shortestPath(vector<vector<int>>& grid, int k) {
vector<vector<int>> dir = {{1,0},{0,1},{0,-1},{-1,0}};
int m = grid.size(), n = grid[0].size(), i, j, ni, nj, step = 0, curs, d;
vector<vector<vector<bool>>> vis(m,vector<vector<bool>>(n, vector<bool>(k+1, false)));
queue<vector<int>> q;
q.push({0,0,0});// i, j, s障碍物搬了几次
vis[0][0][0] = true;
while (!q.empty())
int size = q.size();
while(size--)
i = q.front()[0];
j = q.front()[1];
curs = q.front()[2];
if(i==m-1 && j==n-1)
return step;
q.pop();
for(d = 0; d < 4; ++d)
ni = i+dir[d][0];
nj = j+dir[d][1];
if(ni<0 || ni >= m || nj<0 || nj >= n)
continue;//出界了
if(grid[ni][nj] && curs+1 <= k && !vis[ni][nj][curs+1])//是障碍物, 还能搬走
vis[ni][nj][curs+1] = true;
q.push({ni,nj,curs+1});
else if(!grid[ni][nj] && !vis[ni][nj][curs])//不是障碍物
vis[ni][nj][curs] = true;
q.push({ni,nj,curs});
step++;
return -1;
};
400 ms 41.1 MB
python3 解答
class Solution:
def shortestPath(self, grid: List[List[int]], k: int) -> int:
import queue
dir = [[1,0],[0,1],[0,-1],[-1,0]]
m, n = len(grid), len(grid[0])
step = 0
vis = [[[False]*(k+1) for _ in range(n)] for _ in range(m)]
q = queue.Queue(m*n*(k+1))
q.put([0,0,0])
vis[0][0][0] = True
while not q.empty():
size = q.qsize()
while size > 0:
qt = q.get()
i = qt[0]
j = qt[1]
curs = qt[2]
if i==m-1 and j==n-1:
return step
for d in range(4):
ni = i+dir[d][0]
nj = j+dir[d][1]
if ni<0 or ni>=m or nj<0 or nj>=n:
continue
if grid[ni][nj] and curs+1 <= k and not vis[ni][nj][curs+1]:
vis[ni][nj][curs+1] = True
q.put([ni,nj,curs+1])
elif not grid[ni][nj] and not vis[ni][nj][curs]:
vis[ni][nj][curs] = True
q.put([ni,nj,curs])
size -= 1
step += 1
return -1