-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add the maze ii * impl the maze ii, but failed * fix the maze ii, but still failed * fix the maze ii
- Loading branch information
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,6 @@ class SZeroOneMatrix : public ISolution { | |
} | ||
} | ||
|
||
return std::move(dis); | ||
return dis; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters