Skip to content

Commit

Permalink
The maze ii (#251)
Browse files Browse the repository at this point in the history
* add the maze ii

* impl the maze ii, but failed

* fix the maze ii, but still failed

* fix the maze ii
  • Loading branch information
SKTT1Ryze authored Apr 26, 2024
1 parent 555ec9b commit 1187616
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
93 changes: 93 additions & 0 deletions leetcode-cc/TheMazeII.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <climits>
#include <queue>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(PTheMazeII, 505, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"The Maze II", "", {"BFS"});

class STheMazeII : public ISolution {
public:
size_t problemId() const override { return 505; }
string name() const override {
return ("Solution for " + string("The Maze II"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<
tuple<vector<vector<int>>, pair<int, int>, pair<int, int>>, int>(
{{{
{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},
},
{0, 4},
{4, 4}},
{{
{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},
},
{0, 4},
{3, 2}}},
{12, -1}, [this](auto input) {
return this->shortestDistance(get<0>(input), get<1>(input),
get<2>(input));
});
};
int benchmark() const override { return 0; }

private:
int shortestDistance(const vector<vector<int>>& maze, pair<int, int> start,
pair<int, int> destination) const {
int m = maze.size();
int n = maze[0].size();

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

queue<pair<int, int>> q = {};
q.push(start);

vector<vector<int>> dis(m, vector(n, INT_MAX));
dis[start.first][start.second] = 0;

while (!q.empty()) {
if (destination == q.front()) break;
auto [currentX, currentY] = q.front();
q.pop();

for (const auto& [dirX, dirY] : dirs) {
auto nextX = currentX + dirX;
auto nextY = currentY + dirY;
int delta = 0;

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

nextX -= dirX;
nextY -= dirY;

if (dis[currentX][currentY] + delta < dis[nextX][nextY]) {
dis[nextX][nextY] = dis[currentX][currentY] + delta;
q.push({nextX, nextY});
}
}
}

return dis[destination.first][destination.second] == INT_MAX
? -1
: dis[destination.first][destination.second];
}
};
2 changes: 1 addition & 1 deletion leetcode-cc/ZeroOneMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ class SZeroOneMatrix : public ISolution {
}
}

return std::move(dis);
return dis;
}
};
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
#include "../leetcode-cc/SurroundedRegions.hpp"
#include "../leetcode-cc/SymmetricTree.hpp"
#include "../leetcode-cc/TheMaze.hpp"
#include "../leetcode-cc/TheMazeII.hpp"
#include "../leetcode-cc/TheSkylineProblem.hpp"
#include "../leetcode-cc/ThreeSum.hpp"
#include "../leetcode-cc/ThreeSumClosest.hpp"
Expand Down Expand Up @@ -960,5 +961,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
return std::make_shared<SLowestCommonAncestorBTree>();
});

handle->registerProblem(
[]() -> ArcProblem { return std::make_shared<PTheMazeII>(); });
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<STheMazeII>(); });

return 0;
}

0 comments on commit 1187616

Please sign in to comment.