[LeetCode 787] The Maze

本文探讨了在迷宫中,小球从起点滚动至终点的路径算法。不同于传统BFS,此算法考虑小球滚动至墙边才停止的特性,通过队列和集合记录已访问位置,确保算法高效准确地找到路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling updownleft or rightbut 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

Example 1:

Input:
map = 
[
 [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]
]
start = [0,4]
end = [3,2]
Output:
false

Example 2:

Input:
map = 
[[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]
]
start = [0,4]
end = [4,4]
Output:
true

Notice

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.
5.The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.

 

分析

这道题跟传统的BFS解法有一点不同。传统的BFS解法只需要向向下左右寻找满足要求的点即可。但是这一题里,只有小球撞到墙时才能转向。也就是说我们在向队列中添加坐标时一定是靠墙的坐标。我们可以使用set保留所有已经访问过的坐标,在队列每弹出一个新的坐标时,需要向上下左右一定寻找到靠墙的坐标,然后判断是否在set中,如果没有,则加入队列中。

Code

class Solution {
public:
    /**
     * @param maze: the maze
     * @param start: the start
     * @param destination: the destination
     * @return: whether the ball could stop at the destination
     */
    bool hasPath(vector<vector<int>> &maze, vector<int> &start, vector<int> &destination) {
        // write your code here
        
        int rows = maze.size();
        if (rows == 0)
            return false;
        int cols = maze[0].size();
        maze.insert(maze.begin(), vector<int>(cols, 1));
        maze.push_back(vector<int>(cols, 1));
        rows += 2;
        for (int i = 0; i < rows; i ++)
        {
            maze[i].insert(maze[i].begin(), 1);
            maze[i].push_back(1);
        }
        cols += 2;
        
        int sx = start[0] + 1;
        int sy = start[1] + 1;
        int dx = destination[0] + 1;
        int dy = destination[1] + 1;
        set<pair<int, int>> s;
        queue<pair<int, int>> q;
        q.push(make_pair(sx, sy));
        s.insert(make_pair(sx, sy));
        return findRoute(maze, q, s, dx, dy);
    }
    
    bool findRoute(vector<vector<int>>& maze, queue<pair<int, int>> q, set<pair<int, int>> &s, int dx, int dy)
    {
        while (!q.empty())
        {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
            
            if (x == dx && y == dy)
                return true;
            int tmpx = x;
            int tmpy = y;
            while (maze[tmpx][tmpy] != 1)
            {
                tmpx --;
            }
            if (s.find(make_pair(tmpx+1, tmpy)) == s.end())
            {
                q.push(make_pair(tmpx+1, tmpy));
                s.insert(make_pair(tmpx+1, tmpy));
            }
            tmpx = x;
            tmpy = y;
            while (maze[tmpx][tmpy] != 1)
            {
                tmpx ++;
            }
            if (s.find(make_pair(tmpx-1, tmpy)) == s.end())
            {
                q.push(make_pair(tmpx-1, tmpy));
                s.insert(make_pair(tmpx-1, tmpy));
            }
            tmpx = x;
            tmpy = y;
            while (maze[tmpx][tmpy] != 1)
            {
                tmpy --;
            }
            if (s.find(make_pair(tmpx, tmpy+1)) == s.end())
            {
                q.push(make_pair(tmpx, tmpy+1));
                s.insert(make_pair(tmpx, tmpy+1));
            }
            tmpx = x;
            tmpy = y;
            while (maze[tmpx][tmpy] != 1)
            {
                tmpy ++;
            }
            if (s.find(make_pair(tmpx, tmpy-1)) == s.end())
            {
                q.push(make_pair(tmpx, tmpy-1));
                s.insert(make_pair(tmpx, tmpy-1));
            }
        }
        return false;
    }
};

运行效率

Your submission beats 100.00% Submissions!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值