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.
Example 1:
Input 1: a maze represented by a 2D array 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0 Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4) Output: 12 Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right.
The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
Example 2:
Input 1: a maze represented by a 2D array 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0 Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (3, 2) Output: -1 Explanation: There is no way for the ball to stop at the destination.
Note:
- There is only one ball and one destination in the maze.
- Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
- The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
- The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
public class Solution {
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
if (maze == null || start ==null || destination == null) return ; int[] dx = new int[]{, , , -};
int[] dy = new int[] {, -, ,};
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{start[], start[], });
boolean[][] isVisited = new boolean[maze.length][maze[].length];
int min = Integer.MAX_VALUE; while (!queue.isEmpty()) {
int[] point = queue.poll();
isVisited[point[]][point[]] = true;
if (min!= Integer.MAX_VALUE && point[]>=min) continue; //if step is greater than min, we don't need to continue
if (point[] == destination[] && point[] == destination[]) {
min = Math.min(min, point[]);
}
for (int k=; k<; k++) {
int x = point[];
int y = point[];
int step = point[];
while (x+dx[k]>= && x+dx[k] < maze.length && y+dy[k]>= && y+dy[k]<maze[].length && maze[x+dx[k]][y+dy[k]]==) {
x = x+ dx[k];
y = y+ dy[k];
step++;
}
if (!isVisited[x][y]) {
queue.offer(new int[]{x, y, step});
}
}
} return min == Integer.MAX_VALUE? - : min;
}
}
The Maze II的更多相关文章
- 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,不是 ...
- [LeetCode] 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: 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 迷宫之二
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 ...
- [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 rolli ...
随机推荐
- 002_linuxC++_.h和.c文件
(一)程序修改001_linuxC++之_类的引入 (二)修改成为.h和.c文件 #include <stdio.h> #include "person.h" int ...
- Flutter布局4--Row
Row 简介 mainAxisAlignment:主轴布局方式,row主轴方向是水平方向 crossAxisAlignment: 交叉轴的布局方式,对于row来说就是垂直方向的布局方式 Row 是一个 ...
- luogu 3998 [SHOI2013]发微博 map
考试的时候被卡常了~ code: #include <bits/stdc++.h> #define ll long long #define N 200002 #define setIO( ...
- python 改变函数实参的值
def change(n): n[] = 'Mr Gumby' names = ['Mrs Entity', 'Mrs. Thing'] change(names) print(names) resu ...
- Java集合总结(三):堆与优先级队列
堆 满二叉树:满二叉树是指,除了最后一层外,每个节点都有两个孩子,而最后一层都是叶子节点,都没有孩子. 完全二叉树:完全二叉树不要求最后一层是满的,但如果不满,则要求所有节点必须集中在最左边,从左到右 ...
- python实现随机生成头像
今天遇到如何给用户分配随机头像的问题,想着要在本地放很多图片,有点无聊,就找了一些生成头像的工具.发现gravatar生成图像还不错,挺好玩的. 1.下面上代码 # -*- coding: utf-8 ...
- elasticsearch shield(5.0以下版本 权限认证)
elasticsearch 5.0以下的版本要用到权限控制的话需要使用shield.下载地址: https://www.elastic.co/downloads/shield5.0以上的版本则可以使用 ...
- Flutter移动电商实战 --(24)Provide状态管理基础
Flutter | 状态管理特别篇 —— Provide:https://juejin.im/post/5c6d4b52f265da2dc675b407?tdsourcetag=s_pcqq_aiom ...
- treeview所有节点递归解法及注意!!!!!!!!!!!!!!!!!
好吧 我把所有之前写的都删了,只为这一句话“所有变量切记小心在递归函数内部初始化”,包括:布尔,变量i,等等.至于为什么....递归就是调用自己,你初始化以后的变量,等再次调用的时候又回来了 bool ...
- [go]json序列化
// json字段别名 type User struct { Id int `json:"id,string"` Name string `json:"username& ...