题目地址:https://leetcode-cn.com/problems/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.

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: false Explanation: There is no way for the ball to stop at the destination.

Note:

  1. There is only one ball and one destination in the maze.
  2. Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
  3. 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.
  4. The maze contains at least 2 empty spaces, and both the width and height of the maze won’t exceed 100.

题目大意

由空地和墙组成的迷宫中有一个球。球可以向上下左右四个方向滚动,但在遇到墙壁前不会停止滚动。当球停下时,可以选择下一个方向。
给定球的起始位置,目的地和迷宫,判断球能否在目的地停下。
迷宫由一个0和1的二维数组表示。 1表示墙壁,0表示空地。你可以假定迷宫的边缘都是墙壁。起始位置和目的地的坐标通过行号和列号给出。

解题方法

BFS

类似于505. The Maze II,该题比较简单,只需要求出是否能够停止即可。我们使用BFS来判断所有能停下来的位置,然后看每个能停下来的位置是否于目标位置相同。如果所有可以停下的位置都不包括目标位置,说明不能停在目标位置。

判断能停在哪些位置时,我们可以对一个点的位置进行四个方向的移动,如果遇到墙壁或者到达边界就停止,此即能够移动到的一个位置。然后把这个位置放入队列中,相当于在这个位置当做新的起点,向四个方向移动,看能停在哪些位置。

一般BFS需要有个visited数组,用来判断每个位置是否访问过,从而判断新位置是否加入队列中。在这个做法中,用求hash的方法来确定每个坐标对应的整形,用该整形数字表示坐标。

C++代码如下:

class Solution {
public:
bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
if (maze.empty() || maze[0].empty()) return false;
M = maze.size();
N = maze[0].size();
queue<vector<int>> que;
que.push(start);
unordered_set<int> visited;
while (!que.empty()) {
vector<int> cur = que.front(); que.pop();
visited.insert(cur[0] * N + cur[1]);
for (auto& dir : dirs) {
vector<int> end = rolling(maze, cur, dir);
if (end[0] == destination[0] && end[1] == destination[1])
return true;
if (!visited.count(end[0] * N + end[1])) {
que.push(end);
}
}
}
return false;
}
vector<int> rolling(vector<vector<int>>& maze, vector<int>& start, vector<int>& dir) {
int x = start[0];
int y = start[1];
while (true) {
int nx = x + dir[0];
int ny = y + dir[1];
if (nx < 0 || nx >= M || ny < 0 || ny >= N || maze[nx][ny] == 1) {
return {x, y};
}
x = nx;
y = ny;
}
return start;
}
private:
int M = 0, N = 0;
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
};

日期

2019 年 9 月 22 日 —— 熬夜废掉半条命

【LeetCode】490. The Maze 解题报告 (C++)的更多相关文章

  1. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. Session和Cookie的原理,以及在分布式应用中出现的问题和解决方案

    产生原因 由于http协议是无状态的,同一个浏览器对服务器的两次请求之间是没有关系的,服务器认为两次请求都是全新的请求,不会记住上次请求成功的数据.然而现有的业务常常需要服务器能记住用户的访问情况, ...

  2. 解决Package is not available (for R version XXX)?

    目录 1. 更新R(不推荐) 2. 更改或指定镜像源 3.源码安装 安装R包时这个错误是经常见到的.我认为有几个方法可解决,记录之. 1. 更新R(不推荐) 简单粗暴的方法就是更新R,但这波及的范围太 ...

  3. MEGA软件——系统发育树构建方法(图文讲解) 转载

    转载:http://www.plob.org/2012/12/02/4927.html 一.序列文本的准备 构树之前先将目标基因序列都分别保存为txt文本文件中(或者把所有序列保存在同一个txt文本中 ...

  4. 爬虫动态渲染页面爬取之Splash的介绍和使用

    Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库.利用它,我们同样可以实现动态渲染页面的抓取. 1. 功能介 ...

  5. MySQL-数据库多表关联查询太慢,如何进行SQL语句优化

    工作中我们经常用到多个left join去关联其他表查询结果,但是随着数据量的增加,一个表的数据达到百万级别后,这种普通的left join查询将非常的耗时. 举个例子:  现在porder表有 10 ...

  6. IO流的字节输入输出流(InputStream,OutputStream)

    字节输出流与文件字节输出流 文件存储原理和记事本打开文件原理 OutputStream及FileOutputStream import java.io.FileOutputStream; import ...

  7. SCRDet——对小物体和旋转物体更具鲁棒性的模型

    引言 明确提出了三个航拍图像领域内面对的挑战: 小物体:航拍图像经常包含很多复杂场景下的小物体. 密集:如交通工具和轮船类,在航拍图像中会很密集.这个DOTA数据集的发明者也提到在交通工具和轮船类的检 ...

  8. 为构建大型复杂系统而生的微服务框架 Erda Infra

    作者|宋瑞国(尘醉) 来源|尔达 Erda 公众号 ​ 导读:Erda Infra 微服务框架是从 Erda 项目演进而来,并且完全开源.Erda 基于 Erda Infra 框架完成了大型复杂项目的 ...

  9. jenkins+Gitlab安装及初步使用

    安装包下载地址:https://packages.gitlab.com/gitlab/gitlab gitlab-cerpm 包国内下载地址: https://mirrors.tuna.tsinghu ...

  10. 微服务中心Eureka

    一.简介 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS(AWS 是业务流程管理开发平台AWS Enterprise BPM Platform ...