题目

非常简单的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的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode 1091. Shortest Path in Binary Matrix

    原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...

  5. [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径

    We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...

  6. LeetCode 847. Shortest Path Visiting All Nodes

    题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...

  7. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  8. leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径

    设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...

  9. 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...

随机推荐

  1. 近日LeetCode算法(记录)

    近日LeetCode算法 前言:最近刷了好多leetcode算法题,大家知道,程序=数据结构+算法,由此可见,算法真的是很重要的呢.闲话少谈,切入正题,来看看小编觉得有点意思的5题算法题吧... 1. ...

  2. python中自带的三个装饰器

    说到装饰器,就不得不说python自带的三个装饰器: 1.@property 将某函数,做为属性使用 @property 修饰,就是将方法,变成一个属性来使用. class A(): @propert ...

  3. php 利用curl_*测试数据并发

    工作时遇到一个数据并发问题,因为上线之前没有测试数据并发,导致有时候网络比较差的时候导致数据重复插入数据库 , 所以利用curl_*函数专门写了一个测试数据并发的测试用例,如下: function t ...

  4. 如何给HTML页面设置行高

    设置行高 由于简单还是老样子直接上代码了哦,注意:line-height属性值可以使用固定值如:20px..和百分比如:20%. 如果想让文字垂直居中如下:行高的主要作用是用来设置文本的垂直方向居中对 ...

  5. hitTest和pointInside如何响应用户点击事件

    hitTest和pointInside如何响应用户点击事件 处理机制 iOS事件处理,首先应该是找到能处理点击事件的视图,然后在找到的这个视图里处理这个点击事件. 处理原理如下: • 当用户点击屏幕时 ...

  6. JSON解析(序列化和反序列化)

    JSON的序列化,代码示例: NSDictionary *dic = @{}; if (![NSJSONSerialization isValidJSONObject:dic]) { NSLog(@& ...

  7. postman---postman增加断言

    我们在做测试的时候都会有一个验证点,我们通常把这个验证点叫做断言,断言通过了就会说明我们的这个用例是通过的,当然这么强大的postman也是有断言的,我们一起学习下如何通过postman增加断言. 断 ...

  8. react相关小技巧

    一.我们在项目中切换路由的时候可能会遇到 Warning: setState(...): Can only update a mounted or mounting component. This u ...

  9. CentOS 8安装体验

    这两天出来了,晚上爽一爽. 一,下载 http://ftp.sjtu.edu.cn/centos/8.0.1905/isos/x86_64/ 还是那7G左右的保险,没有minial版了,那个500m多 ...

  10. 使用vue-video-player插件实现视频播放

    来自于https://blog.csdn.net/abelethan/article/details/89016678博客 1下载插件==>npm install vue-video-playe ...