[LC] 490. The Maze
时间:2019-12-06
本文章向大家介绍[LC] 490. The Maze,主要包括[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; } }
原文地址:https://www.cnblogs.com/xuanlu/p/11994474.html
- MyBatis.Net 学习手记
- 基于JavaScript 声明全局变量的三种方式详解
- 网页基础篇之如何制作简单的静态网页
- Mybatis.Net 整合 ODP.NET Managed
- 通过maven test 报org.apache.ibatis.binding.BindingException: Invalid bound statement
- 知道这几点,用微信小程序留住海量客户不是问题
- C#:DataTable映射成Model
- jenkins 多选框
- Oracle:ODP.NET Managed 小试牛刀
- C#:Func的同步、异步调用
- Python之路-day6
- hadoop1.2.1伪分布模式配置
- 有了它们,真的能让孩子赢在起跑线上吗?
- Unity 使用快速教程
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- php面试笔记(5)-php基础知识-自定义函数及内部函数考点
- AJAX的一个简单实例,跨域的解决,使用JQuery来进行ajax的调用
- ASP.NET Core 将文件夹内容输出为压缩包文件方法
- 如何阻止指定类型的SAP CRM附件被上传到服务器
- Docker 之NameSpace与Cgroup
- 微信网页扫码登录和公众号网页授权登录的比较
- 【TBase开源版测评】分布式数据库复制表关联查询
- v-decorator的取值与赋值
- fastjson导致spring security oauth2的token序列化错误
- 微信小程序webview,a锚点跳转,回退时一直保留在原页面
- SLURM使用教程
- MIME 类型大全,你值得收藏
- Jetbrains系列---PyCharm, Goland翻译插件推荐Translation
- 熬夜总结了 “HTML5画布” 的知识点(共10条)
- 在PyTorch中使用深度自编码器实现图像重建