[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 ...
随机推荐
- share团队冲刺6
团队冲刺第六天 昨天:进行各种原件的自定义样式,进行界面布局 登陆界面: 今天:进行后台的代码编写,实现各种按钮的功能 问题:在不同的型号手机上,界面会发生不兼容的问题.
- 搭建zookeeper环境
zookeeper是一个强一致的分布式数据库,由多个节点共同组成一个分布式集群,挂掉任意一个节点,数据库仍然可以正常工作. 独立模式 下载zookeeper打包文件,并进行解压 ➜ ~ tar -xv ...
- Linux学习(二) --- 常用命令
[TOC] 一.常用命令 1.目录 cd切换 cd 路径 切换到指定路径 cd .. 上一级 mkdir:创建目录 mkdir 目录名 pwd:查看 2.查看 ll命令:查询目录内容 ll 查看当前( ...
- Python—快速排序算法
# _*_ coding=utf-8 _*_ """ 快速排序: 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比 另外一部分的所有数据都要小,然后 ...
- Linux-exec族函数
1.为什么需要exec族函数 (1).fork子进程是为了执行新程序(fork创建子进程后,子进程和父进程同时被OS调度执行,因此子程序可以单独的执行一个程序,这样程序宏观上将会和父进程程序同时进行) ...
- Dinic学习笔记
网络流是啥不用我说了吧 增广路定理不用我说了吧 Dinic就是分层然后只在层间转移,然后就特别快,\[O(N^2M)\] 伪代码: function dinic int flow = 0 ; whil ...
- Proe4.0使用VB.net生成缩略图方法
Private Sub btnRasterImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ...
- POJ-2349 Arctic Network(最小生成树+减免路径)
http://poj.org/problem?id=2349 Description The Department of National Defence (DND) wishes to connec ...
- 吴裕雄--天生自然 PYTHON3开发学习:数据库连接 - PyMySQL 驱动
import pymysql # 打开数据库连接 db = pymysql.connect("localhost","testuser","test1 ...
- PAT甲级——1152.Google Recruitment (20分)
1152 Google Recruitment (20分) In July 2004, Google posted on a giant billboard along Highway 101 in ...