diff --git a/leetcode-cc/TheMazeII.hpp b/leetcode-cc/TheMazeII.hpp new file mode 100644 index 0000000..73827ff --- /dev/null +++ b/leetcode-cc/TheMazeII.hpp @@ -0,0 +1,93 @@ +#include +#include + +#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>, pair, pair>, 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>& maze, pair start, + pair destination) const { + int m = maze.size(); + int n = maze[0].size(); + + vector> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + + queue> q = {}; + q.push(start); + + vector> 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]; + } +}; diff --git a/leetcode-cc/ZeroOneMatrix.hpp b/leetcode-cc/ZeroOneMatrix.hpp index a6c6c70..ab80525 100644 --- a/leetcode-cc/ZeroOneMatrix.hpp +++ b/leetcode-cc/ZeroOneMatrix.hpp @@ -67,6 +67,6 @@ class SZeroOneMatrix : public ISolution { } } - return std::move(dis); + return dis; } }; diff --git a/runtime-cc/src/registration.cc b/runtime-cc/src/registration.cc index faef0aa..2fdba64 100644 --- a/runtime-cc/src/registration.cc +++ b/runtime-cc/src/registration.cc @@ -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" @@ -960,5 +961,10 @@ const int registerAll(std::shared_ptr handle) { return std::make_shared(); }); + handle->registerProblem( + []() -> ArcProblem { return std::make_shared(); }); + handle->registerSolution( + []() -> ArcSolution { return std::make_shared(); }); + return 0; }