You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.
Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.
 
       利用深度优先加回溯结果time Limited。。。
class Solution {
public:
int direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int res=INT_MAX; int shortestPath(vector<vector<int>>& grid, int k) {
//这种题感觉就是递归回溯 但是有两个限制 一个是求最小步数(动态规划) 然后是求能够消除指定k个 障碍
//如果不ok就回溯一个
//lets have a try
//好久没写bfs了
int m=grid.size(),n=grid[0].size();
vector<vector<int>> memo(m,vector<int>(n,0));
dfs(grid,memo,0,0,k,m,n,0);
if(res==INT_MAX) return -1;
return res;
} bool inAera(int x,int y,int m,int n){
//judge aera
return x>=0 &&x<m &&y>=0 &&y<n;
}
void dfs(vector<vector<int>>& grid,vector<vector<int>> &memo,int x,int y,int k,int m,int n,int count)
{
if(x==m-1&&y==n-1 &&k>=0)
{
res=min(res,count);
return;
}
if(k<0) return;
//如何剪枝呢?
if(grid[x][y]==1 && k==0) return;
//当前格子做一个标注表示走过了
memo[x][y]=1;
for(int i=0;i<4;++i)
{
int x_next=x+direction[i][0];
int y_next=y+direction[i][1];
if(inAera(x_next,y_next,m,n) && k>=0 &&memo[x_next][y_next]==0){
dfs(grid,memo,x_next,y_next,k-grid[x][y],m,n,count+1);
}
}
memo[x][y]=0;//回溯
return; }
};
  利用队列实现广度优先:
class Solution {
public:
    int direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    int res=INT_MAX;
    
    int shortestPath(vector<vector<int>>& grid, int k) {
        int m=grid.size(),n=grid[0].size();
        vector<vector<int>> memo(m,vector<int>(n,-1));//为啥是-1呢?
        //利用队列实现bfs
        if(k>=m+n-2) return m+n-2; //步数有富裕
        queue<tuple<int,int,int>>ss;
        memo[0][0]=k;
        ss.push({0,0,k});
        int level=0;
        while(!ss.empty()){
            auto sz=ss.size();
            ++level;
            while(sz--){
                //广度优先
                auto[x,y,ck]=ss.front();
                ss.pop();
                for(int i=0;i<4;++i){
                    int x_next=x+direction[i][0];
                    int y_next=y+direction[i][1];
                    if(inAera(x_next,y_next,m,n)){
                        int nk=ck-grid[x_next][y_next];
                        if(nk<0) continue;
                        if(memo[x_next][y_next]>=nk) continue;//不是最短的
                        if(x_next==m-1 && y_next==n-1)
                        {
                            return level;
                        }
                        memo[x_next][y_next]=nk;
                        ss.push({x_next,y_next,nk});
                    }
                    
                }
            }
        }
        return -1;   
    }
    bool inAera(int x,int y,int m,int n){
        //judge aera
        return x>=0 &&x<m &&y>=0 &&y<n;
    }
};

【leetcode】1293 .Shortest Path in a Grid with Obstacles的更多相关文章

  1. 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...

  2. 【leetcode】1129. Shortest Path with Alternating Colors

    题目如下: Consider a directed graph, with nodes labelled 0, 1, ..., n-1.  In this graph, each edge is ei ...

  3. 【leetcode】1091. Shortest Path in Binary Matrix

    题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...

  4. LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination

    题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...

  5. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】64. Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  8. leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]

    题目链接 Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can m ...

  9. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

随机推荐

  1. 服务集与AP的配合

    一.实验目的 1)掌握添加无线网络配置 2)掌握配置信道和协议使用并配置在一个天线上同时运行两个服务集,即两个无线网络 二.实验仪器设备及软件 仪器设备:一台AC,两台AP,一台AR,一台LSW 软件 ...

  2. Centos 8 升级ssl到1.1.1h

    升级到1.1.1h版本 #编译openssl和安装 ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl & ...

  3. FAIL : Keyword 'BuiltIn.Log' expected 1 to 6 arguments, got 12(解决方法)

    RF运行关键字:Run Keyword If ,log输出报错"FAIL : Keyword 'BuiltIn.Log' expected 1 to 6 arguments, got 12. ...

  4. IO流(二)

    一:字符流 字符输入流 写入文件字符流 import java.io.FileWriter; import java.io.IOException; //fileWriter public class ...

  5. Veeam Backup & Replication 10.0.0.4461安装部署(包含补丁)

    Veeam Backup & Replication 是一款数据保护软件,为VMware 和Hyper-V 虚拟机.物理与云环境提供了备份.复制与恢复选项.如有需要请去官方购买正版授权:htt ...

  6. [bzoj3170]松鼠聚会

    这个距离就是切比雪夫距离,有一个神奇的东西是说将(x,y)变成(x+y,x-y),然后就是曼哈顿距离,因此转化后对x坐标和y坐标分别统计排序和求和(求前缀和预处理+二分) 1 #include< ...

  7. idea内存配置

     找到IDEA安装的bin目录 打开idea.exe.vmoptions 文件 如果嫌麻烦还打开了idea 那么就可以点击这个.. 关键的三个参数的说明 1. -Xms 是最小启动内存参数 2. -X ...

  8. emoji表情等特殊字符处理和存储的两个方案

    方案1.改数据库配置 使之支持emoji表情等特殊字符,小公司或者个人开发还好,大公司用此方案代价较大. 以mysql为例,改配置方法参考:https://blog.csdn.net/u0107373 ...

  9. Vulnhub-DarkHole_1 题解

    Vulnhub-DarkHole_1-Writeup 靶机地址:DARKHOLE: 1 Difficulty: Easy 扫描与发现 使用arp-scan发现目标IP arp-scan -l 使用nm ...

  10. 『学了就忘』Linux权限管理 — 53、ACL权限详解

    目录 1.什么是ACL权限 2.开启ACL 3.ACL权限的相关命令 (1)设定ACL权限 (2)查询文件的ACL权限 (3)设置文件ACL权限给用户组 (4)给文件夹和里边的文件同时赋予ACL权限 ...