【LeetCode 1263】 Minimum Moves to Move a Box to Their Target Location

题目描述

Storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations.

The game is represented by a grid of size m x n, where each element is a wall, floor, or a box.

Your task is move the box ‘B’ to the target position ‘T’ under the following rules:

Player is represented by character ‘S’ and can move up, down, left, right in the grid if it is a floor (empy cell).
Floor is represented by character ‘.’ that means free cell to walk.
Wall is represented by character ‘#’ that means obstacle (impossible to walk there).
There is only one box ‘B’ and one target cell ‘T’ in the grid.
The box can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. This is a push.
The player cannot walk through the box.
Return the minimum number of pushes to move the box to the target. If there is no way to reach the target, return -1.

Example 1:

Input: grid = [["#","#","#","#","#","#"],
               ["#","T","#","#","#","#"],
               ["#",".",".","B",".","#"],
               ["#",".","#","#",".","#"],
               ["#",".",".",".","S","#"],
               ["#","#","#","#","#","#"]]
Output: 3
Explanation: We return only the number of times the box is pushed.

Example 2:

Input: grid = [["#","#","#","#","#","#"],
               ["#","T","#","#","#","#"],
               ["#",".",".","B",".","#"],
               ["#","#","#","#",".","#"],
               ["#",".",".",".","S","#"],
               ["#","#","#","#","#","#"]]
Output: -1

Example 3:

Input: grid = [["#","#","#","#","#","#"],
               ["#","T",".",".","#","#"],
               ["#",".","#","B",".","#"],
               ["#",".",".",".",".","#"],
               ["#",".",".",".","S","#"],
               ["#","#","#","#","#","#"]]
Output: 5
Explanation:  push the box down, left, left, up and up.

Example 4:

Input: grid = [["#","#","#","#","#","#","#"],
               ["#","S","#",".","B","T","#"],
               ["#","#","#","#","#","#","#"]]
Output: -1

Constraints:

m == grid.length
n == grid[i].length
1 <= m <= 20
1 <= n <= 20
grid contains only characters ‘.’, ‘#’, ‘S’ , ‘T’, or ‘B’.
There is only one character ‘S’, ‘B’ and ‘T’ in the grid.

思路

状态,箱子所在位置和人所在的位置。
判断重复,用箱子所在位置和上一个位置不重复。
然后每次bfs判断人是否能到达当前需要的位置。

代码

class Solution {
public:
    int minPushBox(vector<vector<char>>& grid) {
        queue<pair<int, int>> que;
        int sx, sy, bx, by;
        int n = grid.size();
        int m = grid[0].size();
        for (int i=0; i<n; ++i) {
            for (int j=0; j<m; ++j) {
                if (grid[i][j] == 'S') {
                    sx = i, sy = j;
                }else if (grid[i][j] == 'B') {
                    bx = i, by = j;
                }
            }
        }
        
        vector<vector<int>> pdirs {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; 
        map<pair<int, int>, int> vis;
        vis[{bx*m+by, sx*m+sy}] = 1;
        que.push({bx*m+by, sx*m+sy});
        
        int step = 0;
        while(!que.empty()) {
            int size = que.size();
            while(size--) {
                int posb = que.front().first;
                int poss = que.front().second;
                que.pop();
                bx = posb / m;
                by = posb % m;
                sx = poss / m;
                sy = poss % m;
                
                if (grid[bx][by] == 'T') return step;
                for (int i=0; i<4; ++i) {
                    int nx = bx + dirs[i][0];
                    int ny = by + dirs[i][1];
                    if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
                    if (grid[nx][ny] == '#') continue;
                    int px = bx + pdirs[i][0];
                    int py = by + pdirs[i][1];
                    if (px < 0 || px >= n || py < 0 || py >= m) continue;
                    if (grid[px][py] == '#') continue;
                    if (vis[{nx*m+ny, px*m+py}]) continue;
                    if (!reach(sx, sy, px, py, grid, bx, by)) continue;
                    vis[{nx*m+ny, px*m+py}] = 1;
                    que.push({nx*m+ny, bx*m+by});
                }
            }
            step++;
        }
        return -1;
    }
    
private:
    vector<vector<int>> dirs {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
    bool reach(int x, int y, int tx, int ty, vector<vector<char>>& grid, int bx, int by) {
        int n = grid.size();
        int m = grid[0].size();
        vector<vector<int>> vis(n, vector<int>(m, 0));
        queue<pair<int, int>> que;
        que.push({x, y});
        vis[x][y] = 1;
        
        while(!que.empty()) {
            int x = que.front().first;
            int y = que.front().second;
            if (x == tx && y == ty) return true;
            que.pop();
            for (int i=0; i<4; ++i) {
                int nx = x + dirs[i][0];
                int ny = y + dirs[i][1];
                if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
                if (vis[nx][ny] == 1) continue;
                if (grid[nx][ny] == '#') continue;
                if (nx == bx && ny == by) continue;
                vis[nx][ny] = 1;
                que.push({nx, ny});
            }
        }
        
        return false;
    }
};

就是一个麻烦点的bfs。。。。

发布了323 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/iCode_girl/article/details/104979851