【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 ...
随机推荐
- cf22A Second Order Statistics(STL-UNIQUE的使用)
题意: N个数,找出第二大的数.如果没有输出-1. 思路: UNIQUE的使用. 代码: int a[105]; int n; int main(){ cin>>n; rep(i,0,n- ...
- Swift-技巧(一)缩放并填充图片
摘要 直接操作图片来实现它的缩放或者填充多余空间,首选 UIGraphicsBeginImageContext 函数来实现,它就相当于一个画布,你甚至可以用它来涂鸦. 最近有一个需求,就是将图片先等比 ...
- Redis监控调研
1 调研目的 主要的目的是想调研各大云平台有关Redis监控功能的实现,但是最后我发现各大云平台提供的监控功能都比较基础,比如我想看诸如访问频率较高的HotKey.占用内存较大的Bigkey等指标,它 ...
- 腾讯发布 K8s 多集群管理开源项目 Clusternet
11月4日,在腾讯数字生态大会上,腾讯宣布了云原生领域一项重磅开源进展-- K8s 多集群管理项目 Clusternet 正式开源. Clusternet 由腾讯联合多点生活.QQ音乐.富途证券.微众 ...
- sqlalchemy create single table
User.__table__.drop(engine) User.__table__.create(engine) https://stackoverflow.com/a/45287771/80250 ...
- 一个开源的C#和cefsharp项目:逐浪字体大师pc版上线(附源码开源)
z01逐浪字体大师,是一款基于C#和web引擎开发的字体设计软件,可以打开直接写字,也可以链接官方资源 ,附Github开源库,欢迎大家下载.客户端技术是基于wpf设计的,整个界面精美,与逐浪CMS技 ...
- windonw10 ,python3.7安装gevent
前言:gevent协程,网上找到安装gevent 需要安装grennlent. 1.首先根据版本下载相应的gevent模块,可以去官方下,我是在这里下载的.http://www.lfd.uci.edu ...
- Python-Unittest多线程生成报告
前言 selenium多线程跑用例,这个前面一篇已经解决了,如何生成一个测试报告这个是难点,刚好在github上有个大神分享了BeautifulReport,完美的结合起来,就能生成报告了. 环境必备 ...
- 理解ASP.NET Core - 错误处理(Handle Errors)
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或[点击此处查看全文目录](https://www.cnblogs.com/xiaoxiaotank/p/151852 ...
- Study Blazor .NET(二)安装
翻译自:Study Blazor .NET,转载请注明. 安装 请根据下面步骤安装开始使用Blazor: 1.针对不同的操作系统,安装最新版.Net Core框架 [这里] 2.用.Net Core ...