Skip to content

Commit

Permalink
impl the maze
Browse files Browse the repository at this point in the history
  • Loading branch information
SKTT1Ryze committed Apr 23, 2024
1 parent 6a26549 commit a28af17
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions leetcode-cc/TheMaze.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class STheMaze : public ISolution {
queue<pair<int, int>> q = {};
q.push(start);
unordered_set<pair<int, int>, pair_hash> memo = {};
vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

while (!q.empty()) {
auto current = q.front();
Expand All @@ -67,14 +68,21 @@ class STheMaze : public ISolution {

memo.insert(current);

for (const auto& [x, y] :
vector<pair<int, int>>({{current.first - 1, current.second},
{current.first + 1, current.second},
{current.first, current.second - 1},
{current.first, current.second + 1}})) {
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == 0 &&
!memo.contains({x, y})) {
q.push({x, y});
for (const auto& [dirX, dirY] : dirs) {
auto nextX = current.first + dirX;
auto nextY = current.second + dirY;

while (nextX >= 0 && nextX < m && nextY >= 0 && nextY < n &&
maze[nextX][nextY] != 1) {
nextX += dirX;
nextY += dirY;
}

nextX -= dirX;
nextY -= dirY;

if (!memo.contains({nextX, nextY})) {
q.push({nextX, nextY});
}
}
}
Expand Down

0 comments on commit a28af17

Please sign in to comment.