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. 力扣 - 剑指 Offer 67. 把字符串转换成整数

    题目 剑指 Offer 67. 把字符串转换成整数 思路1 根据题意,要解决这题,首先要判断的条件有: 不包括首位空格 第一位必须为:+.-.数字三者其一,否则不合法 数字必须连续的,如果遇到非数字, ...

  2. CSS学习笔记:display属性

    目录 一.display属性概述 1. 块级元素和行内元素的区别 2.常见的块级元素和行内元素 3. display属性常见的属性值 二.测试display取各属性值的效果 1. 测试inline和b ...

  3. 文件与文件系统的压缩与打包 tar gzip bzip2

    1:linux下常见的压缩文件后缀: .gz .zip .bz2 打包后的: .tar.gz .tar.zip .tar.bz2 2:gzip: 压缩:gzip file 解压:gunzip file ...

  4. TestNG 参数化应用

    一.第一种参数化方式(testng.xml配置参数) 1.新建ParameterDemo04类 2.拷贝类的路径 3.testng.xml配置类地址及参数 <?xml version=" ...

  5. C++ 函数模板实现原理剖析

    C++ 函数模板实现机制原理剖析 重点 编译器并不是把函数模板处理成能够处理任意类的函数 编译器从函数模板通过具体类型来产生不同的函数 编译器会对函数模板进行两次编译 (1)在声明的位置对模板代码进行 ...

  6. loadrunner12自带的机票预订服务,解决httpd: Could not reliably determine the server's fully qualified domain name 问题

    遇到以上问题是在启动loadrunner12自带的机票预订服务器情况下遇到的,错误提示如下图: 解决方案: 编辑httpd.conf 文件,加入一句 ServerName localhost:1080 ...

  7. go的变量、常量以及判断变量的数据类型

    1.定义变量 p.p1 { margin: 0; font: 12px "Helvetica Neue"; color: rgba(69, 69, 69, 1) } span.s1 ...

  8. FZU ICPC 2020 寒假训练 5 —— 排序

    P1177 [模板]快速排序 题目描述 利用快速排序算法将读入的 N 个数从小到大排序后输出.快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成. ...

  9. halcon语法讲解

    前言 最近换工作,在学习了解halcon工具,每天总结分析今天所学知识,今天是基础语法篇! 1.基本语法 描述 语法 等号 := 不等号 # 注释符 * 字符串赋值 str:='halcon' 等于比 ...

  10. R数据分析:纵向数据如何做中介,交叉滞后中介模型介绍

    看似小小的中介,废了我好多脑细胞,这个东西真的不简单,从7月份有人问我,我多重中介,到现在的纵向数据中介,从一般的回归做法,到结构方程框架下的路径分析法,到反事实框架做法,从中介变量和因变量到是连续变 ...