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

  1. There is only one ball and one hole in the maze.
  2. Both the ball and hole 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 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;    }};


阅读全文
0 0
原创粉丝点击