[LC] 505. The Maze II
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
Given the ball's start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
- class Solution {
- public int shortestDistance(int[][] maze, int[] start, int[] destination) {
- int row = maze.length;
- int col = maze[0].length;
- int res = 0;
- Queue<Cell> queue = new LinkedList<>();
- int[][] dists = new int[row][col];
- for (int[] dist: dists) {
- Arrays.fill(dist, -1);
- }
- queue.offer(new Cell(start[0], start[1]));
- dists[start[0]][start[1]] = 0;
- int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
- while (!queue.isEmpty()) {
- int size = queue.size();
- Cell cur = queue.poll();
- int curX = cur.x;
- int curY = cur.y;
- for (int[] direction: directions) {
- int newX = curX;
- int newY = curY;
- int curDist = dists[curX][curY];
- while (newX >= 0 && newX < row && newY >= 0 && newY < col && maze[newX][newY] == 0) {
- newX += direction[0];
- newY += direction[1];
- curDist += 1;
- }
- newX -= direction[0];
- newY -= direction[1];
- curDist -= 1;
- if (dists[newX][newY] == -1 || curDist < dists[newX][newY]) {
- dists[newX][newY] = curDist;
- queue.offer(new Cell(newX, newY));
- }
- }
- }
- return dists[destination[0]][destination[1]];
- }
- }
- class Cell {
- int x;
- int y;
- public Cell(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
[LC] 505. The Maze II的更多相关文章
- [LeetCode] 505. The Maze II 迷宫之二
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- LeetCode 505. The Maze II
原题链接在这里:https://leetcode.com/problems/the-maze-ii/ 题目: There is a ball in a maze with empty spaces a ...
- [LeetCode] 505. The Maze II 迷宫 II
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- 505. The Maze II
原题链接:https://leetcode.com/articles/the-maze-ii/ 我的思路 在做完了第一道迷宫问题 http://www.cnblogs.com/optor/p/8533 ...
- 【LeetCode】505. The Maze II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- A Dangerous Maze (II) LightOJ - 1395(概率dp)
A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...
- LightOJ - 1395 A Dangerous Maze (II) —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1395 1395 - A Dangerous Maze (II) PDF (English) Statistic ...
- lintcode 787. The Maze 、788. The Maze II 、
787. The Maze https://www.cnblogs.com/grandyang/p/6381458.html 与number of island不一样,递归的函数返回值是bool,不是 ...
- LC 499. The Maze III 【lock,hard】
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
随机推荐
- 单细胞测序|单细胞基因组|单细胞转录组|Gene editing|
单细胞测序 单细胞基因组学 测量理由是单细胞的时间空间特异性. Gene expression&co-expression 比较正常cell与疾病cell,正常organ与疾病organ,看出 ...
- 6.react 基础 - 关于 react 开发 的原则
1. 声明式开发 通过绑定元素 在数据变更时 对元素进行动态渲染 2. 可以与其他框架并存 不在React的绑定元素内, 可以使用其他框架 如 ( vue jQuery 等 ) 进行元素操作 3. 组 ...
- JAVA初学者——DOS命令及基本数据类型
Hello!大家好!我是浩宇大熊猫~ 昨天看了韩顺平老师第二节视频的课,记忆尤新的是那个数据类型那一块,可是昨天感觉没掌握就没有发博客. 今天,又看了一遍,加上看了一些其他老师的,有所收获,所以分享给 ...
- Excel Old format or invalid type library 错误原因
Old format or invalid type library 错误原因 调用excel方法失败,Old format or invalid type library 解决方案: 1,这是Exc ...
- CodeForces 1294B Collecting Packages(排序+贪心)
http://codeforces.com/contest/1294/problem/B 大致题意: 一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否 ...
- Oracle与MySQL的区别对比
本文对数据库Oracle与MySQL进行了区别对比,其中从并发性.一致性.事务.数据持久性等十三方面进行了对比. 本文摘自 51cto 一.并发性 并发性是oltp数据库最重要的特性,但并发涉及到资源 ...
- LeetCode——456.132模式
给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...
- 68)deque数组
基本要求: 1)和vecctor基本区别 示意图 vector在尾部添加和删除, deque在尾部添加和删除,在头部添加和删除. 2)基本知识: 3)deque的构造形式: 4)基本操作和遍历 ...
- mybatis 在自动生成时设置不生成Example类
只需要在配置要生成的table表中添加几个配置属性就行了. 在generatorConfig.xml文件中修改 <!--指定数据库表--> <table tableName=&quo ...
- PAT Basic 1013 数素数 (20) [数学问题-素数]
题目 令Pi表示第i个素数.现任给两个正整数M <= N <= 10^4,请输出PM到PN的所有素数. 输⼊格式: 输⼊在⼀⾏中给出M和N,其间以空格分隔. 输出格式: 输出从PM到PN的 ...