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)
{
this->x=x;
this->y=y;
this->k=k;
this->ans=ans;
}
};
class Solution {
public:
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int vis[105][105];
int vis2[105][105];
int n,m;
vector<vector<int>> map;
queue<Node> q;
int shortestPath(vector<vector<int>>& grid, int k) {
n = grid.size();
m = grid[0].size();
memset(vis,-1,sizeof(vis));
q.push(Node(0,0,k,0));
int ans = -1;
while(!q.empty())
{
Node term = q.front();
q.pop();
if(term.x == n-1 && term.y==m-1)
{
return term.ans;
}
for(int i=0;i<4;i++)
{
int xx = term.x +dir[i][0];
int yy = term.y +dir[i][1];
if(xx < 0 || yy < 0 || xx >= n || yy >=m)
continue;
if(vis[xx][yy]!=-1&&vis[xx][yy]>=term.k)
continue;
if(grid[xx][yy]==1)
{
if(term.k>0)
{
vis[xx][yy]=term.k-1;
q.push(Node(xx,yy,term.k-1,term.ans+1));
}
}
else
{
vis[xx][yy]=term.k;
q.push(Node(xx,yy,term.k,term.ans+1));
}
}
}
return -1;
}
};
LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination的更多相关文章
- 【leetcode】1293 .Shortest Path in a Grid with Obstacles
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You ...
- 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] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
- LeetCode 1091. Shortest Path in Binary Matrix
原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...
- [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...
- LeetCode 847. Shortest Path Visiting All Nodes
题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...
- [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)
题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...
- leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径
设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
随机推荐
- python免密远程执行shell
使用paramiko库:https://github.com/paramiko/paramiko 简单封装SSH类 import paramiko class SSH: def __init__(se ...
- python爬虫:将数据保存到本地
一.python语句存储 1.with open()语句 with open(name,mode,encoding) as file: file.write() name:包含文件名称的字符串; mo ...
- c# Winform 继承窗体 无法拖动修改控件大小
问题描述: 一个窗体集成父窗体,发现无法直接拖动修改的控件,比如修改大小等 特征: 不禁使父窗体控件,就算新加一个控件也会这样:鼠标放到控件移动手方块上会出现一个“继承的控件”的tooptip, 异常 ...
- C#使用Emgu CV来进行图片人脸检测
项目需求:某市级组织考试,在考试前需审核考生采集表中的考生照片是否合格,由于要审核的考生信息采集表有很多,原先进行的是手动人工审核,比较费时费力,审核的要求也很简单,并不判断考生是否是图片本人(身份验 ...
- 用 Python 带你看各国 GDP 变迁
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 周萝卜 源自:萝卜大杂烩 PS:如有需要Python学习资料的小伙伴 ...
- Flask 教程 第四章:数据库
本文翻译自 The Flask Mega-Tutorial Part IV: Database 在Flask Mega-Tutorial系列的第四部分,我将告诉你如何使用数据库. 本章的主题是重中之重 ...
- Python--方法/技巧在哪用的典型例子
就我个人在学习Python的过程中,经常会出现学习了新方法后,如果隔上几天不用,就忘了的情况,或者刚学习的更好的方法没有得到应用,还是沿用已有的方法,这样很不利于学习和掌握新姿势,从而拉长学习时间,增 ...
- 有趣的bug——Java静态变量的循环依赖
背景 是的,标题没有错误,不是Spring Bean的循环依赖,而是静态变量之间的循环依赖. 近期的项目均是简单的Maven项目,通过K8S部署在阿里云上,其配置文件读取规则如下所示: (1) 优先读 ...
- [b0007] windows 下 eclipse 开发 hdfs程序样例
目的: 学习使用hdfs 的java命令操作 相关: 进化: [b0010] windows 下 eclipse 开发 hdfs程序样例 (二) [b0011] windows 下 eclipse 开 ...
- ElasticSearch安装及运行的坑
一.确认centos系统是为64位的,x86的不可以安装 1. 下载elasticsearch包 2. 用 tar -zxvf 解压包 3. 增加一个elk用户,elasticsearch7不可用ro ...