add: 230508-CN
This commit is contained in:
parent
6471f95007
commit
615c8444a3
|
|
@ -0,0 +1,94 @@
|
|||
#include <bitset>
|
||||
#include <queue>
|
||||
#include <cstdint>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* 1263. Minimum Moves to Move a Box to Their Target Location
|
||||
*
|
||||
* A 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 an m x n grid of characters grid where each element is a wall, floor, or box.
|
||||
*
|
||||
* Your task is to move the box 'B' to the target position 'T' under the following rules:
|
||||
*
|
||||
* The character 'S' represents the player. The player can move up, down, left, right in grid if it is a floor (empty cell).
|
||||
* The character '.' represents the floor which means a free cell to walk.
|
||||
* The character '#' represents the wall which means an 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.
|
||||
*/
|
||||
|
||||
class LC230508CN {
|
||||
private:
|
||||
static inline constexpr std::int8_t dX[] = {0, 1, 0, -1};
|
||||
static inline constexpr std::int8_t dY[] = {1, 0, -1, 0};
|
||||
|
||||
public:
|
||||
static int minPushBox(const std::vector<std::vector<char>>&) noexcept;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
constexpr bool is_valid(T x, T y, T m, T n) {
|
||||
return (x >= 0) && (x < m) && (y >= 0) && (y < n);
|
||||
}
|
||||
|
||||
int LC230508CN::minPushBox(const std::vector<std::vector<char>>& G) noexcept {
|
||||
using Status = std::tuple<int, std::int8_t, std::int8_t, std::int8_t, std::int8_t>;
|
||||
using Position = std::pair<std::int8_t, std::int8_t>;
|
||||
const std::int8_t m = G.size(), n = G.front().size();
|
||||
|
||||
std::queue<Status> q;
|
||||
Position target, box_init, player;
|
||||
for (std::int8_t i = 0; i < m; ++i) {
|
||||
for (std::int8_t j = 0; j < n; ++j) {
|
||||
switch (G[i][j]) {
|
||||
case 'T':
|
||||
target = {i, j}; break;
|
||||
case 'B':
|
||||
box_init = {i, j}; break;
|
||||
case 'S':
|
||||
player = {i, j};
|
||||
}
|
||||
}
|
||||
}
|
||||
q.emplace(0, box_init.first, box_init.second, player.first, player.second);
|
||||
|
||||
std::bitset<512> player_vis;
|
||||
std::bitset<262'144> box_vis;
|
||||
std::queue<Position> bfs_q;
|
||||
box_vis[box_init.first * m + box_init.second] = true;
|
||||
while (!q.empty()) {
|
||||
auto [step, cx, cy, px, py] = q.front();
|
||||
q.pop();
|
||||
|
||||
player_vis.reset()[px * n + py] = true;
|
||||
bfs_q.emplace(px, py);
|
||||
while (!bfs_q.empty()) {
|
||||
auto [x, y] = bfs_q.front();
|
||||
bfs_q.pop();
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
std::int8_t x_ = x + dX[i], y_ = y + dY[i];
|
||||
if ((x_ == cx && y_ == cy) || !is_valid(x_, y_, m, n) || G[x_][y_] == '#' || player_vis[x_ * n + y_])
|
||||
continue;
|
||||
player_vis[x_ * n + y_] = true;
|
||||
bfs_q.emplace(x_, y_);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
std::int8_t nx = cx + dX[i], ny = cy + dY[i], npx = cx - dX[i], npy = cy - dY[i];
|
||||
if (!is_valid(nx, ny, m, n) || !is_valid(npx, npy, m, n) || !player_vis[npx * n + npy] || G[nx][ny] == '#' || box_vis[nx * m * n * n + ny * m * n + npx * n + npy])
|
||||
continue;
|
||||
if (nx == target.first && ny == target.second)
|
||||
return step + 1;
|
||||
q.emplace(1 + step, nx, ny, cx, cy);
|
||||
box_vis[nx * m * n * n + ny * m * n + npx * n + npy] = true;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -80,4 +80,13 @@ public:
|
|||
static int diagonalSum(const std::vector<std::vector<int>>&) noexcept;
|
||||
};
|
||||
|
||||
class LC230508CN {
|
||||
private:
|
||||
static inline constexpr std::int8_t dX[] = {0, 1, 0, -1};
|
||||
static inline constexpr std::int8_t dY[] = {1, 0, -1, 0};
|
||||
|
||||
public:
|
||||
static int minPushBox(const std::vector<std::vector<char>>&) noexcept;
|
||||
};
|
||||
|
||||
#endif //LEETCODE_CPP_DEFS_H
|
||||
|
|
|
|||
|
|
@ -12,6 +12,13 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& x) noexcept {
|
|||
}
|
||||
|
||||
int main() {
|
||||
std::cout << LC230507::longestObstacleCourseAtEachPosition({110,2,35,160,121,7,138,7,112,170,42,197,5,35,115,61,149,81,198,82,42,56,36,199,6,74,151,50,13,116,89,23,81,112,19,199,15,100,128,129,22,165,145,12,170,123,74,6,125,114,94,26,176,32,173,37,131,17,29,8,123,53,15,22,160,29,2,158,75,136,104,3,14,126,134,6,183,131,72,183,71,183,157,57,71,24,200,117,109,184,151,84,87,167,57,79,186,122,140,35,164,28,64,123,78,188,15,178,84,165,92,156,77,45,176,167,3,176,107,87,28,31,173,134,43,128,143,109,84,63,123,156,154,157,171,50,94,2,167,9,159,135,55,177,122,142,149,194,83,37,198,148,66,90,41,64,198,142,92,178,4,152,118,182,91,135,29,197,185,84,124,134,100,49,124,140,131,156,165,194,1,74,83,58,52,101,91,28,183,93,31,144,107,133,110,83,43,16,119,146,35,57,13,91,125,141,149,105,175,47,38,96,94,95,169,55,156,11,70,36,52,103,64,164,77,2,40,126,173,155,164,120,125,38,178,49,27,53,63,128,27,140,110,13,100,181,38,8,88,140,12,103,173,155,53,169,137,1,106,1,132,169,17,42,55,17,135,151,93,164,22,110,144,200,86,13,94,198,177,142,97,2,154,57,188,130,150,83,63,128,22,179,75,21,173,32,185,197,29,25,107,87,158,111,91,137,155,175,80,120,40,88,19,160,74,199,65,14,133,106,76,28,162,192,2,71,200,140,174,82,181,79,117,101,146,146,65,118,35,125,105,84,137,196,73,119,4,51,61,111,66,37,93,85,83,48,148,147,46,81,8,134,73,3,116,35,64,103,109,20,199,119,143,161,35,84,143,34,168,54,178,150,43,144,85,118,38,97,39,101,68,47,139,85,200,109,45,96,185,47,156,183,167,48,91,169,6,108,147,183,145,77,133,111,143,160,31,127,17,186,172,89,104,182,107,157,130,86,182,42,121,113,38,13,138,69,194,195,105,81,107,152,115,107,166,43,75,154,180,115,18,195,22,62,122,144,200,142,137,196,153,90,199,85,9,51,143,46,174,26,103,167,140,116,35,113,162,86,48,120,100,102,197,177,186,11,13,138,45,91,66,2,3,53,33,148,15,8,117,97,93,9,199,24,161,184,140,78,111,90,110,84,193,142,48,106,114,69,127,38,69,79,180,56,8,81,79,28,12,185,50,9,69,59,41,36,92,129,24,100,64,14,57,75,62,22,3,52,67,105,180,143,137,47,53,151,116,100,162,86,139,39,148,26,50,48,116,178,8,141,171,56,28,28,166,177,122,114,195,3,34,161,132,98,146,44,194,87,184,76,62,9,178,33,94,165,122,100,20,171,138,197,108,48,64,74,64,108,60,20,104,173,69,81,31,12,2,146,62,27,170,181,123,12,4,28,153,6,5,126,191,32,140,28,175,155,176,90,112,137,38,108,106,195,84,67,10,3,123,173,72,130,191,2,39,112,42,190,81,167,60,196,90,20,110,57,199,114,162,130,108,190,9,25,27,11,165,170,49,93,171,169,180,108,177,118,128,122,29,48,70,86,138,29,34,39,79,146,88,6,38,105,42,35,60,6,87,199,25,150,101,93,194,139,126,193,85,59,60,50,180,155,1,186,183,120,92,177,155,124,136,98,122,61,173,180,67,114,62,135,41,4,54,149,100,61,18,95,46,70,53,40,58,97,85,116,34,1,6,82,120,170,26,116,159,45,185,122,93,92,54,101,33,21,175,94,133,61,38,162,172,80,56,175,197,167,123,65,196,85,131,107,116,31,41,185,114,9,24,166,123,136,176,16,96,58,19,90,61,110,194,193,57,109,29,82,111,13,137,112,20,71,178,92,136,199,101,158,101,111,49,73,15,166,59,72,22,136,8,126,137,177,44,43,17,66,96,66,86,61,89,72,102,167,11,193,132,141,59,145,54,127,156,25,100,35,36,173,52,23,9,92,14,162,185,146,86,1,181,125,152,153,52,150,57,12,14,166,128,151,151,121,125,110,145,150,143,133,152,75,152,175,87,186,42,90,141,139,161,68,154,78,68,58,140,8,50,83,172,150,183,42,57,122,52,97,139,2,190,114,111,198,155,29,135,88,183,76,18,157,121,158,95,36,171,54,142,150,25,76,71,8,108,115,165,155,168,25,170,181,135,195,191,30,55,122,128,159,196,197,153,88,194,131,111,178,64,10,146,142,113,30,153,188,199,138,157,141,7,63,199,71,94,158,136,141,182,99,168,65});
|
||||
std::cout << LC230508CN::minPushBox(
|
||||
{{'#','.','.','#','#','#','#','#'},
|
||||
{'#','.','.','T','#','.','.','#'},
|
||||
{'#','.','.','.','#','B','.','#'},
|
||||
{'#','.','.','.','.','.','.','#'},
|
||||
{'#','.','.','.','#','.','S','#'},
|
||||
{'#','.','.','#','#','#','#','#'}}
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue