leetcode 1263 -- Minimum Moves to Move a Box to Their Target Location

https://leetcode.com/problems/minimum-moves-to-move-a-box-to-their-target-location/
https://zxi.mytechroad.com/blog/searching/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.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Solution {
public:
struct State {
int bx, by;
int px, py;

State(int _bx, int _by, int _px, int _py) {
bx = _bx; by = _by;
px = _px; py = _py;
}

int key() {
return ((bx * 20 + by) << 16) | (px * 20 + py);
}

void print() {
printf("bx=%d, by=%d, px=%d, py=%d\n", bx, by, px, py);
}
};

int minPushBox(vector<vector<char>>& grid) {
int row = grid.size(), col = grid[0].size();

int tx, ty, bx, by, px, py;
findPos(grid, tx, ty, bx, by, px, py);

queue<State> q;
unordered_set<int> seen;

State state(bx, by, px, py);
q.push(state);
seen.insert(state.key());

int step = 0;

vector<int> dirs = {0, -1, 0, 1, 0};
vector<int> pdirs = {0, 1, 0, -1, 0};

while (!q.empty()) {
int size = q.size();

while (size--) {
State curr = q.front(); q.pop();
//curr.print();

if (curr.bx == tx && curr.by == ty) return step;

for (int i = 0; i < 4; i++) {
State next(curr.bx + dirs[i], curr.by + dirs[i+1], curr.bx, curr.by);

if (next.bx < 0 || next.bx >= row || next.by < 0 || next.by >= col
|| grid[next.bx][next.by] == '#'
|| seen.count(next.key())) continue;

if (!hasPath(grid, curr, curr.bx + pdirs[i], curr.by + pdirs[i+1])) continue;

seen.insert(next.key());
q.push(next);
}
}

step++;
}

return -1;
}

void findPos(vector<vector<char>>& grid, int &tx, int &ty, int &bx, int &by, int &px, int &py) {
int row = grid.size(), col = grid[0].size();

for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == 'T') {
tx = i; ty = j;
} else if (grid[i][j] == 'B') {
bx = i; by = j;
} else if (grid[i][j] == 'S') {
px = i; py = j;
}
}
}
}

bool hasPath(vector<vector<char>>& grid, State &curr, int tx, int ty) {
int row = grid.size(), col = grid[0].size();
int x = curr.px, y = curr.py;

queue<pair<int,int>> q;
unordered_set<int> seen;

q.push({x,y});
seen.insert(x * row + y);

vector<int> dirs = {0, -1, 0, 1, 0};

while (!q.empty()) {
auto p = q.front(); q.pop();

if (p.first == tx && p.second == ty) return true;

for (int i = 0; i < 4; i++) {
x = p.first + dirs[i];
y = p.second + dirs[i+1];

int key = x * row + y;
if (x < 0 || x >= row || y < 0 || y >= col || grid[x][y] == '#' || seen.count(key)) continue;
if (x == curr.bx && y == curr.by) continue;

seen.insert(key);
q.push({x, y});
}
}

return false;
}
};