[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 ...
随机推荐
- 面试准备 HTTP协议
http协议的主要特点 简单快速 //某个资源是固定的 (统一资源符)UII 灵活 //http头部有个数据类型,完成不同数据类型的传输 无连接 //链接一次就会断开 无状态 //客户端和服务端 ...
- c语言的各种技巧
一.参考网址 1.参考网址1:C 语言的奇技淫巧 二.
- iOS中copy和=的区别
copy是浅拷贝即指针拷贝,让对象的引用计数加一 在MRC环境下,=只是简单的指针指向,引用对象的引用计数并不会增加,在用局部变量赋值时很容易出现野指针 在ARC环境下,=的效果等同于copy和ret ...
- EXCEL启动慢
启动太慢,一般是加载项的问题. 1.点击文件-EXCEL选项 2. 找到加载项,一般是COM加载项 3.选择com加载项 4.然后我出现了无法更改的情况,于是,我做了以下调整,进入office安装目录 ...
- 云托管,边缘物理计算&托管物理计算,你所需要了解的……
随着业务发展,传统数据中心建设复杂性越来越高,基建的管理.设备的繁杂.人力成本的提升,是否让你的运维成本越来越高?企业生产效率却越来越低? 业务快速发展,设备采购周期冗长,大量采购造成CAPEX过重, ...
- 69)stack和queue操作
操作和vector类似 直接看 vector就行了 或者看笔记 C++进阶课程讲义
- drf框架知识点总复习
接口 """ 1.什么是接口:url+请求参数+响应数据 | 接口文档 2.接口规范: url:https,api,资源(名词复数), v1,get|post表示操作资源 ...
- ccf201403-3 记录一个神tmwa了的代码 莫非我没看懂题。。。
#include <string.h> #include<cstdio> #include<stdio.h> #include <iostream> # ...
- Python笔记_第一篇_面向过程_第一部分_6.语句的嵌套
学完条件控制语句和循环控制语句后,在这里就会把“语言”的精妙之处进行讲解,也就是语句的嵌套.我们在看别人代码的时候总会对一些算法拍案叫绝,里面包含精妙和精密的逻辑分析.语句的嵌套也就是在循环体内可以嵌 ...
- SQL Link Oracle
转自:http://www.2cto.com/database/201107/96105.html 做项目过程中常用到数据库同步,现把前一段时间做的一个项目部分,同步过程贴出来,供分享与自己参考! 本 ...