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 ...
随机推荐
- WPF线段式布局的一种实现
线段式布局 有时候需要实现下面类型的布局方案,不知道有没有约定俗成的称呼,我个人强名为线段式布局.因为元素恰好放置在线段的端点上. 实现 WPF所有布局控件都直接或间接的继承自System.Windo ...
- Vue中v-on的指令以及一些其他指令
1.v-on的基本使用 <div id="app"> <!-- 使用事件绑定的简写形式 --> <input type="button&qu ...
- 用canvas写一个简易画图工具
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- opencv::基于颜色跟踪检测
基于颜色跟踪 inRange过滤 形态学操作提取 轮廓查找 外接矩形获取 位置标定
- 从《华为的冬天》到AI的冬天 | 甲子光年
知难不难,惶者生存. 作者 | DougLong 编辑 | 火柴Q.甲小姐 *本文为甲子光年专栏作家DougLong独家稿件.作者为AI从业者.Gary Marcus<Rebooting AI& ...
- LeetCode——Delete Duplicate Emails(巧用mysql临时表)
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- [b0023] python 归纳 (九)_html解析-lxml
# -*- coding: utf-8 -*- """ 学习lxml解析网页 程序功能: 解析 360影视 电影排行榜中的信息 https://www.360kan.co ...
- [Go] golang实现mysql连接池
golang中连接mysql数据库,需要使用一个第三方类库github.com/go-sql-driver/mysql,在这个类库中就实现了mysql的连接池,并且只需要设置两个参数就可以实现 一般连 ...
- vue-router 之 keep-alive路由缓存处理include+exclude
keep-alive 简介 keep-alive 是 Vue 内置的一个组件,可以使被包含的组件保留状态,或避免重新渲染. 用法也很简单: <keep-alive> <compone ...
- pushgateway
下载pushgateway wget https://github.com/prometheus/pushgateway/releases/download/v0.9.0/pushgateway-0. ...