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 ...
随机推荐
- Javase之多线程(1)
多线程(1) 多线程的概述 了解多线程之前需要先了解线程,而线程依赖于进程而存在,所以先了解进程. 什么是进程 进程就是正在运行的程序.是系统进行资源分配和调用的独立单位.每一个进程都有它自己的内存空 ...
- Gradle task简单使用
还望支持个人博客站:http://www.enjoytoday.cn task是什么 task是gradle构建脚本的最小运行单元,我们通过在gradle脚本中创建task任务,以期完成某个特定的功能 ...
- MySQL 时间类型 DATE、DATETIME和TIMESTAMP
1.DATE.DATETIME和TIMESTAMP 表达的时间范围 Type Range Remark DATE '1000-01-01' to '9999-12-31' 只有日期部分,没有时间部分 ...
- 钉钉开发第三方H5微应用入门详细教程[ISV][免登流程][授权码][HTTP回调推送][识别用户身份][获取用户信息]
转载请注明原文地址:https://www.cnblogs.com/applerosa/p/11509512.html (by lnexin@aliyun.com 世间草木) 此教程注意点: 适用于第 ...
- Mysql—用户表详解(mysql.user)
MySQL是一个多用户管理的数据库,可以为不同用户分配不同的权限,分为root用户和普通用户,root用户为超级管理员,拥有所有权限,而普通用户拥有指定的权限. MySQL是通过权限表来控制用户对数据 ...
- 14.Java基础_函数/函数重载/参数传递
Java函数和函数重载 /* 函数定义: public static 返回类型 func(参数){ 方法体: } 函数重载 在调用时,Java虚拟机会通过参数的不同来区分同名的函数 满足: 1.多个函 ...
- 初学JavaScript正则表达式(一)
给单个单词is改为大写的IS \bis\b // \b指的是单词边界 IS He is a boy This is a test isn't it 给以http://开头并且以jpg结尾的链接删除掉h ...
- 浅谈AMD与CMD
AMD 是 RequireJS 在推广过程中对模块定义的规范化产出. CMD 是 SeaJS 在推广过程中对模块定义的规范化产出. 这些规范的目的都是为了 JavaScript 的模块化开发,特别是在 ...
- 面向对象程序设计(JAVA) 第8周学习指导及要求
2019面向对象程序设计(Java)第8周学习指导及要求 (2019.10.18-2019.10.21) 学习目标 掌握接口定义方法: 掌握实现接口类的定义要求: 掌握实现了接口类的使用要求: 理解 ...
- SpringMVC详细流程(一)
Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模 ...