首页 > 其他分享 >【BFS】LeetCode 1091. 二进制矩阵中的最短路径

【BFS】LeetCode 1091. 二进制矩阵中的最短路径

时间:2023-01-15 13:22:24浏览次数:59  
标签:1091 int BFS length grid Pair new nextX LeetCode

题目链接

1091. 二进制矩阵中的最短路径

思路

BFS 找最短路模板题

代码

class Solution {
    public int shortestPathBinaryMatrix(int[][] grid) {
        if(grid[0][0] == 1 || grid[grid.length - 1][grid[0].length - 1] == 1){
            return -1;
        }

        Queue<Pair<Pair<Integer, Integer>, Integer>> queue = new LinkedList<>();
        boolean[][] visited = new boolean[grid.length][grid[0].length];

        queue.offer(new Pair<>(new Pair<>(0, 0), 1));
        int[] dx = new int[]{1, 0, -1, 0, 1, 1, -1, -1};
        int[] dy = new int[]{0, 1, 0, -1, 1, -1, 1, -1};
        while(!queue.isEmpty()){
            Pair<Pair<Integer, Integer>, Integer> currentState = queue.remove();
            int currentStep = currentState.getValue();
            Pair<Integer, Integer> currentPos = currentState.getKey();
            if(currentPos.getKey() == grid.length - 1 && currentPos.getValue() == grid[0].length - 1){
                return currentStep;
            }

            for(int i = 0; i < 8; i++){
                int nextX = currentPos.getKey() + dx[i];
                int nextY = currentPos.getValue() + dy[i];

                if(valid(nextX, nextY, visited, grid)){
                    queue.offer(new Pair<>(new Pair<>(nextX, nextY), currentStep + 1));
                    visited[nextX][nextY] = true;
                }
            }
        }

        return -1;
    }

    private boolean valid(int nextX, int nextY, boolean[][] visited, int[][] grid) {
        return (
                0 <= nextX && nextX < grid.length && 0 <= nextY && nextY < grid[0].length &&
                        !visited[nextX][nextY] && grid[nextX][nextY] == 0
                );
    }
}

标签:1091,int,BFS,length,grid,Pair,new,nextX,LeetCode
From: https://www.cnblogs.com/shixuanliu/p/17053359.html

相关文章

  • LeetCode刷题:runtime error: reference binding to null pointer of type 'int' (stl_
    题目:https://leetcode.cn/problems/merge-intervals/错误代码://思路初探:做了很多道类似区间操作的题目了。本题就是尽可能少的创建新区间//1.首先对区间排序(左边界升......
  • LeetCode寻找两个正序数组的中位数(vector/二分查找 划分数组)
    原题解题目给定两个大小分别为m和n的正序(从小到大)数组nums1和nums2。请你找出并返回这两个正序数组的中位数。算法的时间复杂度应该为O(log(m+n))。约束......
  • Leetcode——双向链表和哈希表实现LRU缓存和LFU缓存
    一、Leetcode146.LRU缓存1.使用STLlist和unordered_map实现usingKV=pair<int,int>;classLRUCache{private:intcacheCapacity;list<KV>cacheList;......
  • LeetCode.19 删除链表的倒数第n个元素
    1.题目给你一个链表,删除链表的倒数第 ​​n​​ 个结点,并且返回链表的头结点。 2.代码/***Definitionforsingly-linkedlist.*publicclassListNode{*int......
  • LeetCode 142_环形链表 II
    LeetCode142:环形链表II题目给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回null。如果链表中有某个节点,可以通过连续跟踪next指......
  • leetcode 笔记
    1.移位运算符>>&>>=和|=属于位运算符,作用是对二进制数进行移位操作<<左移:末尾补0,原数乘2比如十进制数10,在末位补0等于100,相当于原数乘10,所以x<<1就是......
  • LeetCode.24 两两交换链表中的结点
    1.题目给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 2.代码/***Definitionforsin......
  • leetcode 每日一题 gcd+枚举
    1819.序列中不同最大公约数的数目给你一个由正整数组成的数组nums。数字序列的最大公约数定义为序列中所有整数的共有约数中的最大整数。  例如,序列[4,6,16]......
  • leetcode算法入门 Day5 双指针(四)
    876.链表的中间结点给定一个头结点为head的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。输入:[1,2,3,4,5]输出:此列表中的结点3(序......
  • LeetCode.206 反转链表
    1.题目:给你单链表的头节点 ​​head​​ ,请你反转链表,并返回反转后的链表​输入:head=[1,2,3,4,5]输出:[5,4,3,2,1]*intval;*ListNodenext;*ListNode......