leetcode 1293. Shortest Path in a Grid with Obstacles Elimination(网格中的最短路径)

题目概述

解题思路

参考了题解的做法,我们采用广度优先搜索的方式来解决问题。题目中说到,我们可以破坏至多K个障碍,所以我们在做搜索的时候,需要维护除了当前节点坐标(x, y)之外的另一维度的信息,就是当前剩余的破坏障碍的次数C。我们的目标就是,在C变为负值前,从(0, 0)出发能访问到(M - 1, N - 1)。由于是广度优先搜索,我们第一次访问到该节点时,走过的距离就是最短路径。

方法的时间复杂度是O(MNK)。

方法性能

示例代码

class Nodes
{
    public:
    int x, y, rest, steps;
    Nodes(int xi, int yi, int resti, int si): x(xi), y(yi), rest(resti), steps(si) {}
};

int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

class Solution {
public:
    int shortestPath(vector<vector<int>>& grid, int k) 
    {
        int R_len = grid.size(),
            C_len = grid[0].size();
        if(R_len == 1 && C_len == 1)
            return 0;
        queue<Nodes> bfs;
        bool vis[R_len][C_len][k + 1];
        memset(vis, false, sizeof(vis));

        bfs.push(Nodes(0, 0, k, 0));
        while(bfs.size())
        {
            Nodes temp = bfs.front();
            bfs.pop();

            for (int di = 0, xi, yi; di < 4; ++di)
            {
                xi = temp.x + dir[di][0];
                yi = temp.y + dir[di][1];
                //check the visibility of nodes
                if(xi < 0 || xi >= R_len || yi < 0 || yi >= C_len)
                    continue;
                
                //check if we are at the target node
                if(xi == R_len - 1 && yi == C_len - 1)
                    return temp.steps + 1;
                
                //check if we can visit this node with cost
                if(temp.rest < grid[xi][yi])
                    continue;
                //check the visiting history of node
                if(vis[xi][yi][temp.rest - grid[xi][yi]])
                    continue;
                bfs.push(Nodes(xi, yi, temp.rest - grid[xi][yi], temp.steps + 1));
                vis[xi][yi][temp.rest - grid[xi][yi]] = true;
            }
        }

        return -1;
    }
};

其实我们无需存储节点的步长,不过这时我们需要写一个循环,以确保当前步长的所有节点都已经被遍历过。

class Nodes
{
    public:
    int x, y, rest;
    Nodes(int xi, int yi, int resti): x(xi), y(yi), rest(resti) {}
};

int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

class Solution {
public:
    int shortestPath(vector<vector<int>>& grid, int k) 
    {
        int R_len = grid.size(),
            C_len = grid[0].size(),
            layer_len = 1, STEP = 0;
        if(R_len == 1 && C_len == 1)
            return 0;
        queue<Nodes> bfs;
        bool vis[R_len][C_len][k + 1];
        memset(vis, false, sizeof(vis));

        bfs.push(Nodes(0, 0, k));
        while(bfs.size())
        {
            layer_len = bfs.size();
            STEP++;
            for (int li = 0; li < layer_len;++li)
            {
                Nodes temp = bfs.front();
                bfs.pop();

                for (int di = 0, xi, yi; di < 4; ++di)
                {
                    xi = temp.x + dir[di][0];
                    yi = temp.y + dir[di][1];
                    //check the visibility of nodes
                    if(xi < 0 || xi >= R_len || yi < 0 || yi >= C_len)
                        continue;
                    
                    //check if we are at the target node
                    if(xi == R_len - 1 && yi == C_len - 1)
                        //return temp.steps + 1;
                        return STEP;

                    //check if we can visit this node with cost
                    if(temp.rest < grid[xi][yi])
                        continue;
                    //check the visiting history of node
                    if(vis[xi][yi][temp.rest - grid[xi][yi]])
                        continue;
                    bfs.push(Nodes(xi, yi, temp.rest - grid[xi][yi]));
                    vis[xi][yi][temp.rest - grid[xi][yi]] = true;
                }
            }
        }

        return -1;
    }
};

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值