[Leetcode] 499. The Maze III 解题报告
来源:互联网 发布:网络虚拟电话号码 编辑:程序博客网 时间:2024/06/05 10:30
题目:
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right(r), but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also a hole in this maze. The ball will drop into the hole if it rolls on to the hole.
Given the ball position, the hole position and the maze, find out how the ball could drop into the hole by moving the shortest distance. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the hole (included). Output the moving directions by using 'u', 'd', 'l' and 'r'. Since there could be several different shortest ways, you should output the lexicographically smallest way. If the ball cannot reach the hole, output "impossible".
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 ball and the hole coordinates are represented by row and column indexes.
Example 1
Input 1: a maze represented by a 2D array0 0 0 0 01 1 0 0 10 0 0 0 00 1 0 0 10 1 0 0 0Input 2: ball coordinate (rowBall, colBall) = (4, 3)Input 3: hole coordinate (rowHole, colHole) = (0, 1)Output: "lul"Explanation: There are two shortest ways for the ball to drop into the hole.The first way is left -> up -> left, represented by "lul".The second way is up -> left, represented by 'ul'.Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul".
Example 2
Input 1: a maze represented by a 2D array0 0 0 0 01 1 0 0 10 0 0 0 00 1 0 0 10 1 0 0 0Input 2: ball coordinate (rowBall, colBall) = (4, 3)Input 3: hole coordinate (rowHole, colHole) = (3, 0)Output: "impossible"Explanation: The ball cannot reach the hole.
Note:
- There is only one ball and one hole in the maze.
- Both the ball and hole 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 the width and the height of the maze won't exceed 30.
思路:
这道题目也是DFS和BFS可以解决的,不过确实比[Leetcode] 490. The Maze 解题报告要难不少。这主要是因为我们必须返回最短路径,并且是字典序最小的那个。我这里先贴一个DFS的解决方案,后面有空再补充BFS的。
基本思路也是很直观的,也就是在搜索的过程中维护一个截止当前的最优解(需要包含路径字符串已经路径长度)。在当前的位置上沿着当前方向一直前进,直到遇到障碍。如果在前进的过程中遇到了洞,那么就更新结果。否则当走到尽头的时候就改变方向(只能右转或者左转,不可继续或者逆向返回)。我们在深度优先搜索的过程中,保持down->left->right->up这样一个方向,可以保证字典序最小的结果最小被获得。
我大概想了一下,BFS的思路应该也是类似的,基于DFS的思路稍微变动即可。只不过可能采用PriorityQueue这种数据结构可能会效率更高一些。
代码:
class Solution {public: string findShortestWay(vector<vector<int>>& maze, vector<int>& ball, vector<int>& hole) { pair<string, int> res = {"impossible", INT_MAX}; return roll(maze, ball[0], ball[1], hole, 0, 0, 0, "", res); }private: string roll(vector<vector<int>>& maze, int row, int col, const vector<int>& hole, int d_row, int d_col, int steps, const string &path, pair<string, int> &res) { if (steps < res.second) { if (d_row != 0 || d_col != 0) { // check the position ball + {d_row, d_col} while ((row + d_row) >= 0 && (col + d_col) >= 0 && (row + d_row) < maze.size() && (col + d_col) < maze[0].size() && maze[row + d_row][col + d_col] != 1) { row += d_row; col += d_col; ++steps; if (row == hole[0] && col == hole[1] && steps < res.second) { res = {path, steps}; } } } if (maze[row][col] == 0 || steps + 2 < maze[row][col]) { maze[row][col] = steps + 2; // '1' is for the walls. We change the maze data when performing DFS if (d_row == 0) { // can only start rolling down if the previous direction is horizontal roll(maze, row, col, hole, 1, 0, steps, path + "d", res); } if (d_col == 0) { // can only start rolling left if the previous direction is vertical roll(maze, row, col, hole, 0, -1, steps, path + "l", res); } if (d_col == 0) { // can only start rolling right if the previous direction is vertical roll(maze, row, col, hole, 0, 1, steps, path + "r", res); } if (d_row == 0) { // can only start rolling up if the previous direction is horizontal roll(maze, row, col, hole, -1, 0, steps, path + "u", res); } } } return res.first; }};
- [Leetcode] 499. The Maze III 解题报告
- [Leetcode] 490. The Maze 解题报告
- [Leetcode] 505. The Maze II 解题报告
- LeetCode-Contains Duplicate III-解题报告
- LeetCode-Combination Sum III-解题报告
- [leetcode] 216. Combination Sum III 解题报告
- [leetcode] 260. Single Number III 解题报告
- [leetcode] 220. Contains Duplicate III 解题报告
- [leetcode] 337. House Robber III 解题报告
- [leetcode] 248. Strobogrammatic Number III 解题报告
- LeetCode 260. Single Number III 解题报告
- LeetCode 337. House Robber III 解题报告
- LeetCode 216. Combination Sum III 解题报告
- [leetcode] 437. Path Sum III 解题报告
- 【LeetCode】Path Sum III 解题报告
- [Leetcode] 216. Combination Sum III 解题报告
- [Leetcode] 220. Contains Duplicate III 解题报告
- [Leetcode] 248. Strobogrammatic Number III 解题报告
- 文件系统的简单操作
- 如何让boostrap的栅栏系统高度不坍塌
- 在windows下使用Xming+Putty显示Linux下软件图形界面
- requirejs入门到精通
- angular的基本语法——打破高难度入门观念 (基础篇2)
- [Leetcode] 499. The Maze III 解题报告
- 前端业务代码工具库
- mySql存储过程和函数
- Qt_QLineEdit
- 创投日报:12月5日收录投融资项目15起
- pip install xxx报错SyntaxError: invalid syntax
- CXF +Spring 开发 WebService
- 马云回应“乌镇饭局”:他们没请我;美拍禁止未成年人直播;传陆金所明年赴港IPO丨价值早报
- 小米营销总监:中国模式能复制,但要有本地化的独特性