490. The Maze

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.

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: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.

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: false
Explanation: There is no way for the ball to stop at the destination.

Note:

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

S: DFS O(mn)

  1. 球会一直朝一个方向滚动直到遇到障碍物(中途不会停)
  2. destination需要是能停下的点

DFS模拟球的运动,存储已有的的暂停点避免重复计算

class Solution {
public:
    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        m = maze.size();
        if (m == 0) return false;
        n = maze[0].size();
        visited = vector<vector<bool>>(m, vector<bool>(n, false));
        visited[start[0]][start[1]] = true;
        for (int i = 0; i < 4; ++i) {
            if (dfs(maze, start, destination, i)) return true;
        }
        return false;
    }

private:
    vector<int> dx{0, 1, 0 ,-1};
    vector<int> dy{1, 0, -1, 0};
    vector<vector<bool>> visited;
    int m, n;
    bool dfs(vector<vector<int>>& maze, vector<int> start, vector<int> dest, int dir) {
        while (valid(start) && maze[start[0]][start[1]] == 0) {
            start[0] += dx[dir];
            start[1] += dy[dir];
        }
        start[0] -= dx[dir];
        start[1] -= dy[dir];
        if(start[0] == dest[0] && start[1] == dest[1]) return true;
        if (visited[start[0]][start[1]]) return false;
        visited[start[0]][start[1]] = true;
        for (int i = 0; i < 4; ++i) {
            if (dfs(maze, start, dest, i)) return true;
        }
        return false;
    }

    bool valid(vector<int> pos) {
        return pos[0] >= 0 && pos[0] < m && pos[1] >= 0 && pos[1] < n;
    }
};

505. 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.

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.

S1: BFS

从start开始向四个方向滚动,找到可以达到的stop点,若该点当前length小于原来length入queue,需要重新计算

    int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        int m = maze.size();
        if (m == 0) return 0;
        int n = maze[0].size();
        vector<vector<int>> dist(m, vector<int>(n, -1));
        dist[start[0]][start[1]] = 0;
        queue<pair<int, int>> q;
        q.push({start[0], start[1]});
        vector<int> dx{0, 1, 0, -1};
        vector<int> dy{1, 0, -1, 0};
        while (!q.empty()) {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
            for (int i = 0; i < 4; ++i) {
                int x1 = x, y1 = y;
                int len = -1;                
                while (x1 >= 0 && x1 < m && y1 >= 0 && y1 < n && maze[x1][y1] == 0) {
                    x1 += dx[i];
                    y1 += dy[i];
                    len++;
                }
                x1 -= dx[i];
                y1 -= dy[i];
                if (dist[x1][y1] == -1 || dist[x1][y1] > len + dist[x][y]) {
                    q.push({x1, y1});
                    dist[x1][y1] = dist[x][y] + len;
                }
            }
        }
        return dist[destination[0]][destination[1]];
    }

results matching ""

    No results matching ""