【leetcode】1293 .Shortest Path in a Grid with Obstacles
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的更多相关文章
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- 【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 ...
- 【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- ...
- 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) ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 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 ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
随机推荐
- hdu 1069 Monkey and Banana(记忆搜)
题意: N(不超过30)种木块,每种木块有长.宽.高x,y,z. 木块A可以搭在木块B上当且仅当A的底面长和宽都分别小于B的顶面的长与宽,即不能有超出B的部分. 问垒起来的"木块塔" ...
- Vue-cli4.xPC端项目Rem适配
适配准备 安装 (amfe-flexible) 和(postcss-px2rem) 1, 安装依赖并在main.js中引入该依赖 npm i amfe-flexible import "am ...
- [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <Evaluate> at src/views/index/
关于vue报错: [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly ...
- 将python代码转化为c语言代码,提高运行效率
将python代码转化为c语言代码,提高运行效率 首先,需要安装cpython库: pip install cython 安装完成之后,写一段简单的代码,例如下面这个利用递归求斐波那契数列的函数,然后 ...
- idea离线安装lombok插件
1.查看自己idea版本,2019.1.2,必须安装相同版本的插件 2.从http://plugins.jetbrains.com/plugin/6317-lombok-plugin中下载对应版本的l ...
- 新技能GET!在前端表格中花式使用异步函数的奥义
背景 60年代时,操作系统中独立运行的单元通常是进程.但随着计算机技术的发展,人们发现在进程运行过程中,创建.撤销与切换都要花费较大的时空开销. 到了80年代为了解决这一问题,出现了更小的独立运行基本 ...
- Java安全之基于Tomcat的通用回显链
Java安全之基于Tomcat的通用回显链 写在前面 首先看这篇文还是建议简单了解下Tomcat中的一些概念,不然看起来会比较吃力.其次是回顾下反射中有关Field类的一些操作. * Field[] ...
- [hdu7097]Just a Data Structure Problem
(四边形不等式的套路题) 对于某一组$a_{i}$,显然可以区间dp,设$f_{l,r}$表示区间$[l,r]$的答案,则转移即$$f_{l,r}=\begin{cases}0&(l=r)\ ...
- [loj3302]信号传递
由于n较大,可以将n个数中的关系对数量记录在$m*m$的矩阵中,记作$a[i][j]$ 考虑朴素的状压dp枚举排列,即$f[i]$表示以i中的数的一种排列为整个序列的前缀的最小代价,然后转移枚举下一个 ...
- 【Java面试题】-- Java基本类型
Java基本类型 2019-11-03 19:03:48 by冲冲 1.两个float型相减丢失精度,如何解决? 使用BigDemical装饰器模式 public class Test { pub ...