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 ...
 
随机推荐
- MySQL(11)---约束
			
MySQL(11)---约束 含义: 一种限制,用于限制表中的数据,为了保证表中的数据的准确和可靠性. 先把Mysql几种约束列出来: 主键约束 外键约束 唯一性约束 非空约束 默认值约束 自增约束 ...
 - 使用C#+Edge (Chromium)进行Web自动化测试
			
今天看到了VisualStudio中现在已经自带了Web单元测试项目模板,便试了一下,发现还比较好用,它默认的是Selenium实现的,测试组在用Selenium+Python来写过自动化测试,原来它 ...
 - qt 界面去掉系统边框
			
该代码在Qt5框架编辑,使用该类时, 直接继承这个类就可以了. 实现了拖拽功能和关闭功能,如果需要放大缩小功能, 需自己实现. 1 #ifndef CUSTOMIZE_QWIDGET_H #defin ...
 - 帝国CMS标签【操作类型】说明详解
			
看标签的参数时候,一般最后一个参数是操作类型说明,可是后面写的是:"操作类型说明 具体看操作类型说明", 这个操作类型说明在什么地方看啊 操作类型 说明 操作类型 说明 0 各栏目 ...
 - GALAXY OJ 	NOIP2019联合测试2-普及组
			
概要: 今天比了个赛,还挺水,只不过不太理想. 题目: Problem : 韬韬抢苹果 又到了收获的季节,树上结了许多韬韬,错了,是许多苹果,有很多个小韬韬都来摘苹果.每个韬韬都想要最大的苹果,所以发 ...
 - layui 集成第三方和自定义组件到模块规范
			
1.新建一个layui.extend.js文件,页面调用时这个文件放到layui.js后面. 2.基础的配置卸载config中,扩展的组件写入extend,组件的路径是相对于config下base的路 ...
 - 【转载】如何在Android中避免创建不必要的对象
			
在编程开发中,内存的占用是我们经常要面对的现实,通常的内存调优的方向就是尽量减少内存的占用.这其中避免创建不必要的对象是一项重要的方面. Android设备不像PC那样有着足够大的内存,而且单个App ...
 - git本地忽略
			
添加本地忽略文件 git update-index --assume-unchanged 忽略的文件名 恢复本地忽略文件 git update-index --no-assume-unchanged ...
 - 炫彩字and鼠标爱心
			
<!DOCTYPE html> <style type="text/css"> body{ background-color: black; } #zx { ...
 - BayaiM__MySQL错误对照表
			
BayaiM__MySQL错误对照表 原创 作者:bayaim 时间:2016-06-16 09:16:29 33 0删除编辑 ------------------------------------ ...