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 ...
随机推荐
- cmd命令详解
这几天用了一下Windows系统的“黑框”,即win+R键,发现有些命令都忘了,还得查,就总结了一下: cmd命令 CMD命令:开始->运行->键入cmd或command(在命令行里可以看 ...
- vs code搭建Django环境
在网上找了很多博客,看了vs code的官方文档,最终拼凑起来,终于搭建起来了djangode开发虚拟环境(win10下) 一.新建项目文件夹 F:\Python\temp\django_demo(例 ...
- Web前端基础(6):CSS(三)
1. 定位 定位有三种:相对定位.绝对定位.固定定位 1.1 相对定位 现象和使用: 1.如果对当前元素仅仅设置了相对定位,那么与标准流的盒子什么区别. 2.设置相对定位之后,我们才可以使用四个方向的 ...
- python网络爬虫进阶之HTTP原理,爬虫的基本原理,Cookies和代理介绍
目录 一.HTTP基本原理 (一)URI和URL (二)超文本 (三)HTTP和HTTPS (四)HTTP请求过程 (五)请求 1.请求方法 2.请求的网址 3.请求头 4.请求体 (六)响应 1.响 ...
- Java生鲜电商平台-商品分类表和商品类型表的区别与数据库设计
Java生鲜电商平台-商品分类表和商品类型表的区别与数据库设计 二者服务的对象不一样 目的也是不一样的 商品分类是为商品服务的 用来管理商品 商品类型是为扩展属性服务的 用来管理属性 举例:[转] ...
- 关于spring,IOC和AOP的解析原理和举例
引用自:https://blog.csdn.net/paincupid/article/details/43152397 IOC:就是DAO接口的实现不再是业务逻辑层调用工厂类去获取,而是通过容器(比 ...
- CSTC-2017-Web-writeup
0x01 前言 这一次的比赛web题只做出来3个,也是菜的抠脚.. 0x02 web-签到题 php弱类型 查看源码,发现是代码审计,要求用户名必须为字母,密码必须为数字,登陆页面可以用开头为0 ...
- 微信小程序 + Bmob后端云
闲暇之余,写了一个私人的小程序,但由于带有商品.订单功能被拒了(腾讯太狗带了,只有商家才可以使用这种功能),没办法,不给过审,那就拿出来分享一下. 原本想的是做一个超市类的电商平台,带有下单支付等功能 ...
- 工具类ToastUtil 避免在子线程中使用抛异常 "Can't create handler inside thread that has not called Looper.prepare()"
package com.example.kbr.utils; import android.view.Gravity; import android.widget.Toast; import io.r ...
- 【cf741】D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
传送门 题意: 给出一颗以\(1\)为根的有根树,树边带有一个字符(\(a\)~\(v\))的信息. 输出对于每个结点,其子树内最长的简单路径并且满足边上的字符能够组成回文串. 思路: 显然最终的答案 ...