[Leetcode] 490. The Maze 解题报告
来源:互联网 发布:mongodb js脚本 编辑:程序博客网 时间:2024/06/05 00:13
题目:
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 array0 0 1 0 00 0 0 0 00 0 0 1 01 1 0 1 10 0 0 0 0Input 2: start coordinate (rowStart, colStart) = (0, 4)Input 3: destination coordinate (rowDest, colDest) = (4, 4)Output: trueExplanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
Example 2
Input 1: a maze represented by a 2D array0 0 1 0 00 0 0 0 00 0 0 1 01 1 0 1 10 0 0 0 0Input 2: start coordinate (rowStart, colStart) = (0, 4)Input 3: destination coordinate (rowDest, colDest) = (3, 2)Output: falseExplanation: There is no way for the ball to stop at the destination.
Note:
- There is only one ball and one destination in the maze.
- Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
- 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.
- The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
思路:
我开始把题目的意思给理解错了:必须是球能够停下来的地方才算是能够到达。如果球仅仅经过该格子,但是停不下来,那么就不算能够到达。这道题目用DFS或者BFS都可以解决,也算是DFS和BFS的一个小变种而已。由于能够停下来的位置及其有限,所以我们改用set来记录已经访问过的格子(当然用二维数组记录也是可以的)。此外,我们设计一个go2End函数,来计算球朝某个方向滚动时的下一个停靠点。下面就看看DFS和BFS分别怎么来做了。
1、DFS:对于DFS而言,我们首先判断是否已经到达了destination,如果是则直接返回;否则就看该位置是否已经被访问过了,如果是则返回,否则就记当前位置已经被访问(如果没有这一步,将会导致DFS无法结束)。然后就分别用DFS尝试四个不同方向,有任何一个方向可以到达destination就直接返回true。
2、BFS:对于BFS而言,思路类似。我们首先建立一个队列,并且将start压入队列。每次从队列头部拿出当前位置。如果该位置是destination则直接返回,否则就看该位置是否还没有被访问,如果没有被访问,就置为已访问,并将其四个方向上移动可以停靠的点加入队列。如果队列都空了还没有发现可以到达的路径,那么就说明无法到达了。
代码:
1、DFS:
class Solution {public: bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) { set<vector<int>> visited; return DFS(maze, start, destination, visited); }private: bool DFS(vector<vector<int>> &maze, vector<int> &start, vector<int> &destination, set<vector<int>> &visited) { if(start == destination) { return true; } if(visited.find(start) != visited.end()) { // already visited return false; } visited.insert(start); vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; for(int i = 0; i < 4; ++i) { vector<int> res = go2End(maze, start, dirs[i]); if(res == destination || DFS(maze, res, destination, visited)) { return true; } } return false; } vector<int> go2End(vector<vector<int>> &maze, vector<int> start, vector<int> &dir) { vector<int> new_start = {start[0] + dir[0], start[1] + dir[1]}; int row_num = maze.size(), col_num = maze[0].size(); if(new_start[0] < 0 || new_start[0] >= row_num || new_start[1] < 0 || new_start[1] >= col_num || maze[new_start[0]][new_start[1]] == 1) { return start; } return go2End(maze, new_start, dir); }};
2:BFS:
class Solution {public: bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) { set<vector<int>> visited; queue<vector<int>> q; q.push(start); vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; while (!q.empty()) { vector<int> pos = q.front(); q.pop(); if (pos == destination) { return true; } if (visited.find(pos) == visited.end()) { // not visited yet visited.insert(pos); for(int i = 0; i < 4; ++i) { vector<int> res = go2End(maze, pos, dirs[i]); q.push(res); } } } return false; }private: vector<int> go2End(vector<vector<int>> &maze, vector<int> start, vector<int> &dir) { vector<int> new_start = {start[0] + dir[0], start[1] + dir[1]}; int row_num = maze.size(), col_num = maze[0].size(); if(new_start[0] < 0 || new_start[0] >= row_num || new_start[1] < 0 || new_start[1] >= col_num || maze[new_start[0]][new_start[1]] == 1) { return start; } return go2End(maze, new_start, dir); }};
- [Leetcode] 490. The Maze 解题报告
- [Leetcode] 499. The Maze III 解题报告
- [Leetcode] 505. The Maze II 解题报告
- URAL 1386 Maze 解题报告
- 【POJ 2157】 Maze 解题报告
- LeetCode-The Skyline Problem-解题报告
- [leetcode] 277. Find the Celebrity 解题报告
- [leetcode] 218. The Skyline Problem 解题报告
- [leetcode] 389. Find the Difference 解题报告
- leetcode 389. Find the Difference 解题报告
- 【LeetCode】Find the Difference 解题报告
- 【LeetCode】Construct the Rectangle 解题报告
- 【LeetCode】566.Reshape the Matrix解题报告
- [Leetcode] 218. The Skyline Problem 解题报告
- [Leetcode] 277. Find the Celebrity 解题报告
- [Leetcode] 389. Find the Difference 解题报告
- [Leetcode] 466. Count The Repetitions 解题报告
- [Leetcode] 486. Predict the Winner 解题报告
- Java企业面试算法新得体会之钟--比较有趣的问题20问
- Android源代码分支、版本号、支持设备列表
- 关于高位字节与低位字节简洁明了的说明
- 选择排序初步之 简单选择排序
- execution will delete the plan text.
- [Leetcode] 490. The Maze 解题报告
- FMexample实现中问题汇总
- 一个wordcount程序轻松玩转MapReduce编程模型
- Android微信/QQ红包自动抢(AccessibilityService)
- HDOJ1164 Eddy's research I
- pwnable.tw总结
- 【Scikit-Learn 中文文档】二十七:经网络模型(无监督)- 无监督学习
- 冒泡排序中数据交换的次数
- 关于二维码ji 中的图片中间的二维码大小控制