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. 30分钟通过Kong实现.NET网关

    什么是Kong Openrestry是一个基于Nginx与Lua的高性能平台,内部有大量的Lua库.其中ngx_lua_moudule使开发人员能使用Lua脚本调用Nginx模块.Kong是一个Ope ...

  2. uni-app使用wx-canvas实现微信小程序上显示地图map和坐标geo

    源码 <template> <view class="echart-box"> <canvas class="ec-canvas" ...

  3. (四)php连接apache ,使用php-fpm方式

    上面各篇记录了编译安装lamp的各个部分,下面主要解决php和apache的连接问题.通过 php-fpm 连接. 连接前环境检查: php -v PHP 5.6.30 (cli) (built: O ...

  4. prometheus(7)之数据类型与PromQL语法

    Prometheus的四种数据类型 counter (只增不减 计数) Gauge (常规数值 可变化大小) histogram (柱状图 小于上边界的 总数与次数) summary (分位数  小于 ...

  5. webpack 之 js语法检查eslint

    webpack 之 js语法检查eslint // 用来拼接绝对路径的方法 const {resolve} = require('path') const HtmlWebpackPlugin = re ...

  6. sklearn之转换器和估计器

    sklearn之转换器和估计器 转换器 估计器(sklearn机器学习算法的实现) 转换器 想一下之前做的特征工程的步骤? 实例化(实例化的是一个转换器类(Transformer)--特征工程的父类) ...

  7. Json跨域登录问题的之Access-Control-Allow-Origin 站点跨域请求的问题

    跨域调用json问题 闲暇之时,做了一个博客站点,站点发布网络之后程序功能完成,最后发现了一个跨域的问题,比如我使用abc.com打开系统,一切正常,后台没有任何文件请求报错问题,然后我又使用了www ...

  8. [atARC110F]Esoswap

    先构造使得$p_{i}$降序(即$p_{i}=n-1-i$),只需要从后往前,不断执行$i$操作直至合法即可 正确性的证明:首先保证了$[0,n-i)$这些数字都已经出现,因此操作不会破坏已确定的数字 ...

  9. Java8-JVM内存区域划分白话解读

    前言 java作为一款能够自动管理内存的语言,与传统的c/c++语言相比有着自己独特的优势.虽然我们无需去管理内存,但为了防范可能发生的异常,我们需要对java内部数据如何存储有一定了解,已应对突发问 ...

  10. lilypond进阶——用scheme修改乐谱细节

    lilypond对乐谱内容的修改非常自由,用户可以自由根据需要做调整 调整一般都是用\override的命令,但是会比较冗长,码代码的时候比较麻烦 重新设置一个函数来概括命令,调用的时候使用的代码更短 ...