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:

  1. Input 1: a maze represented by a 2D array
  2.  
  3. 0 0 1 0 0
  4. 0 0 0 0 0
  5. 0 0 0 1 0
  6. 1 1 0 1 1
  7. 0 0 0 0 0
  8.  
  9. Input 2: start coordinate (rowStart, colStart) = (0, 4)
  10. Input 3: destination coordinate (rowDest, colDest) = (4, 4)
  11.  
  12. Output: true
  13.  
  14. Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
  1. class Solution {
  2. public boolean hasPath(int[][] maze, int[] start, int[] destination) {
  3. int row = maze.length;
  4. int col = maze[0].length;
  5. Queue<Cell> queue = new LinkedList<>();
  6. boolean[][] visited = new boolean[row][col];
  7. queue.offer(new Cell(start[0], start[1]));
  8. visited[start[0]][start[1]] = true;
  9. int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  10. while (!queue.isEmpty()) {
  11. Cell cur = queue.poll();
  12. int curX = cur.x;
  13. int curY = cur.y;
  14. for (int[] direction: directions) {
  15. int newX = curX;
  16. int newY = curY;
  17. while (newX >= 0 && newX < row && newY >= 0 && newY < col && maze[newX][newY] == 0) {
  18. newX += direction[0];
  19. newY += direction[1];
  20. }
  21. newX -= direction[0];
  22. newY -= direction[1];
  23. if (visited[newX][newY]) {
  24. continue;
  25. }
  26. if (newX == destination[0] && newY ==destination[1]) {
  27. return true;
  28. }
  29. queue.offer(new Cell(newX, newY));
  30. visited[newX][newY] = true;
  31. }
  32. }
  33. return false;
  34. }
  35. }
  36.  
  37. class Cell {
  38. int x;
  39. int y;
  40. public Cell(int x, int y) {
  41. this.x = x;
  42. this.y = y;
  43. }
  44. }

[LC] 490. The Maze的更多相关文章

  1. [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 ...

  2. LeetCode 490. The Maze

    原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...

  3. 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 ...

  4. [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 ...

  5. 490. The Maze

    原题链接:https://leetcode.com/articles/the-maze/ 这道题目是需要冲会员才能使用的,然而我个穷逼现在还是失业状态根本冲不起...以后如果把免费题目都刷完了的话,再 ...

  6. 【LeetCode】490. The Maze 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 计算机网络(7): 传输层TCP和UDP以及TCP的工作方式

    UDP:无连接:不保证可靠:面向报文的: TCP:面向连接:提供可靠交付:面向字节流(把应用层的数据分包,每个包装一些字节:不关心应用层给的包多大,而是根据网络状况,窗口大小决定) TCP报文: 序号 ...

  2. 负载均衡配置篇(Nginx)

    负载均衡 == 分身的能力. 既然要有分身的能力嘛,这好办,多弄几台服务器就搞定了.今天我们讲的实例嘛…..我们还是先看图比较好: 还是图比较清晰,以下我都用别名称呼: PA : 负载均衡服务器/WE ...

  3. java使用forEach填充字典值

    // 填充字典值 Vector vector = vectorMapper.selectByPrimaryKey(id); VectorModel vectorModel = new VectorMo ...

  4. 数据分析-Numpy-Pandas

    补充上一篇未完待续的Numpy知识点 索引和切片 数组和标量(数字)之间运算 li1 = [ [1,2,3], [4,5,6] ] a = np.array(li1) a * 2 运行结果: arra ...

  5. http协议笔记(不全)

    1.URL 统一资源定位系统 URL由三部分组成:资源类型.存放资源的主机域名.资源文件名.url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址 ...

  6. 个人训练记录(UPD 9.16)

    本文章记录一些较难的题,摘自自己的blog中的其他文章.也有些单独成章有点浪费的题也写在里面了. 2019.7.15-2019.7.21 1182F(2900) 题意:求在区间 \([a,b]\) 中 ...

  7. 小白学习之pytorch框架(6)-模型选择(K折交叉验证)、欠拟合、过拟合(权重衰减法(=L2范数正则化)、丢弃法)、正向传播、反向传播

    下面要说的基本都是<动手学深度学习>这本花书上的内容,图也采用的书上的 首先说的是训练误差(模型在训练数据集上表现出的误差)和泛化误差(模型在任意一个测试数据集样本上表现出的误差的期望) ...

  8. Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之字典类型(dict)

    字典!在Python中字典是另一种可变容器模型,可以存储任意类型的对象.是Python存储数据类型的最高级(maybe). 特点:1. 字典的存储方式和其他类型数据略有不同,是通过键(key)和值(v ...

  9. Codeforces Round #316 (Div. 2) D计算在一棵子树内某高度的节点

    题:https://codeforces.com/contest/570/problem/D 题意:给定一个以11为根的n个节点的树,每个点上有一个字母(a~z),每个点的深度定义为该节点到11号节点 ...

  10. Mac中Apache常用命令

    Apache常用命令记录,还是记一下吧,总是忘记. Apache常用命令: # sudo apachectl start // 启动Apache服务 # sudo apachectl stop // ...