[LC] 490. The Maze
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, determine whether the ball could stop at the destination.
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: true Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int row = maze.length;
int col = maze[0].length;
Queue<Cell> queue = new LinkedList<>();
boolean[][] visited = new boolean[row][col];
queue.offer(new Cell(start[0], start[1]));
visited[start[0]][start[1]] = true;
int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!queue.isEmpty()) {
Cell cur = queue.poll();
int curX = cur.x;
int curY = cur.y;
for (int[] direction: directions) {
int newX = curX;
int newY = curY;
while (newX >= 0 && newX < row && newY >= 0 && newY < col && maze[newX][newY] == 0) {
newX += direction[0];
newY += direction[1];
}
newX -= direction[0];
newY -= direction[1];
if (visited[newX][newY]) {
continue;
}
if (newX == destination[0] && newY ==destination[1]) {
return true;
}
queue.offer(new Cell(newX, newY));
visited[newX][newY] = true;
}
}
return false;
}
} class Cell {
int x;
int y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
}
[LC] 490. The Maze的更多相关文章
- [LeetCode] 490. The Maze 迷宫
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- LeetCode 490. The Maze
原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...
- 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 ...
- [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 ...
- 490. The Maze
原题链接:https://leetcode.com/articles/the-maze/ 这道题目是需要冲会员才能使用的,然而我个穷逼现在还是失业状态根本冲不起...以后如果把免费题目都刷完了的话,再 ...
- 【LeetCode】490. The Maze 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- [LeetCode] 490. The Maze_Medium tag: BFS/DFS
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- [LeetCode] 499. The Maze III 迷宫 III
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 迷宫 II
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
随机推荐
- Paper Review: Epigenetic Landscape, Cell Differentiation 02
I'll share another review paper about Epigenetic Landscape, it comes from Nature Review, published i ...
- android studio 3.2 查看Deveice Monitor
View菜单下面的 Tool Windows 下面的 Devecie File Explorer
- 剑指offer【11】- 矩形覆盖
题目:我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 在分析前不知道是什么序列,所以先看了n=1,n=2,n=3,n= ...
- POJ 1O17 Packets [贪心]
Packets Description A factory produces products packed in square packets of the same height h and of ...
- CodeForces 990B Micro-World(思维、STL)
http://codeforces.com/problemset/problem/990/B 题意: 有n个细菌,每个细菌的尺寸为ai,现在有以常数k,如果细菌i的尺寸ai大于细菌j的尺寸aj,并且a ...
- Linux shell脚本 基础
一.shell中三个引号的用法 1.单引号:所见即所得 例如:var=123 var2='${var}123' echo var2 var2结果为${var}123 2.双引号:输出引号中的内容,若存 ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:循环神经网络预测正弦函数
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 定义RNN的参数. HIDDEN_SIZE = ...
- js时间与日期
var box = new Date(); //创建了一个日期对象:构造方法里面可以传参数,指定时间.如果没有传,就是默认当前时间alert(box); alert(Date.parse('4/12/ ...
- << 和>> 的计算公式
在java中,一个数左移n位,就是将这个数乘以2的n次方,右移就是将这个数除以2的n次方. 如: 8>>2 = 2 (8/2^2) 15 << 3 = 120 (15*(2 ...
- Nginx_配置文件解读及模板
Nginx 介绍 # nginx的最大优势在于处理静态文件和代理转发功能,支持7层负载均衡和故障隔离. 动静分离是每个网站发展到一定规模之后必然的结果.静态请求则应当最好将其拆分,并启用独立的域名,既 ...