gpt4 book ai didi

505. The Maze II 迷宫 II

转载 作者:大佬之路 更新时间:2024-01-31 14:18:50 25 4
gpt4 key购买 nike

题目地址:https://leetcode-cn.com/problems/the-maze-ii/

题目描述

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, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.

Themaze 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: 12

Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right.
             The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.

 

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

Explanation: There is no way for the ball to stop at the destination.

 

Note:

1、 Thereisonlyoneballandonedestinationinthemaze.;
2、 Boththeballandthedestinationexistonanemptyspace,andtheywillnotbeatthesamepositioninitially.;
3、 Thegivenmazedoesnotcontainborder(liketheredrectangleintheexamplepictures),butyoucouldassumetheborderofthemazeareallwalls.;
4、 Themazecontainsatleast2emptyspaces,andboththewidthandheightofthemazewon'texceed100.;

题目大意

小球可以向某个方向一直滚动,当撞到边缘或者墙壁时才停止,每次停止时才可以选择下个移动的方向。问小球是否能从起点出发恰好停在目的地,并返回小球滚动到目的地需要的最少步数。

解题方法

BFS

题目要我们求最少的移动步数,很显然我们使用BFS解决。一般的迷宫问题只会移动一个格子,但是这个题目要求我们撞到墙壁才停止,所以我们需要遍历四个方向,判断四个方向分别撞到墙壁时移动的步数和结束位置。当小球移动到该结束位置总的步数比历史上所有的位置都小,该结束位置放入队列中。

一般BFS需要有个visited数组,用来判断每个位置是否访问过,从而判断新位置是否加入队列中。但是这个题目不需要,因为只有当到达一个结置位置总的步数比以前到达这个位置的步数小的时候,才会加入队列,所以是有限制条件的,结果会是有限的,不会无限循环下去。

代码里使用了dp作为访问每个位置的最小步数,默认是INT_MAX。从起点开始,计算出小球能访问到的所有位置,直至再运动已经不能让所有可以停止的点的访问步数缩小时停止。最后返回结束位置的步数。

C++代码如下:

class Solution {
public:
    int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        const int M = maze.size();
        const int N = maze[0].size();
        vector<vector<int>> dp(M, vector<int>(N, INT_MAX));
        queue<vector<int>> que;
        que.push(start);
        dp[start[0]][start[1]] = 0;
        int count = 0;
        while (!que.empty()) {
            int size = que.size();
            vector<int> start = que.front(); que.pop();
            for (auto dir : dirs) {
                vector<int> end(2, 0);
                int step = rolling(maze, dir, start, end);
                if (dp[end[0]][end[1]] > dp[start[0]][start[1]] + step) {
                    dp[end[0]][end[1]] = dp[start[0]][start[1]] + step;
                    que.push(end);
                }
            }
        }
        return dp[destination[0]][destination[1]] == INT_MAX ? -1 : dp[destination[0]][destination[1]];
    }
private:
    vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    int rolling(vector<vector<int>>& maze, vector<int>& curdir, vector<int>& start, vector<int>& end) {
        end = start;
        int step = 0;
        while (true) {
            int nxt_x = end[0] + curdir[0];
            int nxt_y = end[1] + curdir[1];
            if (nxt_x < 0 || nxt_x >= maze.size() || nxt_y < 0 || nxt_y >= maze[0].size() 
                || maze[nxt_x][nxt_y] == 1) {
                break;
            }
            end[0] = nxt_x;
            end[1] = nxt_y;
            step ++;
        }
        return step;
    }
};

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

参考资料:https://leetcode-cn.com/problems/the-maze-ii/solution/c-bfs-by-sheng-ben-xin/

2022

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com