-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
search_maze.cc
128 lines (107 loc) · 3.96 KB
/
search_maze.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <istream>
#include <string>
#include <vector>
#include "test_framework/generic_test.h"
#include "test_framework/serialization_traits.h"
#include "test_framework/test_failure.h"
#include "test_framework/timed_executor.h"
using std::vector;
enum class Color { kWhite, kBlack };
struct Coordinate;
bool SearchMazeHelper(const Coordinate&, const Coordinate&,
vector<vector<Color>>*, vector<Coordinate>*);
bool IsFeasible(const Coordinate&, const vector<vector<Color>>&);
struct Coordinate {
bool operator==(const Coordinate& that) const {
return x == that.x && y == that.y;
}
int x, y;
};
vector<Coordinate> SearchMaze(vector<vector<Color>> maze, const Coordinate& s,
const Coordinate& e) {
vector<Coordinate> path;
SearchMazeHelper(s, e, &maze, &path);
return path;
}
// Perform DFS to find a feasible path.
bool SearchMazeHelper(const Coordinate& cur, const Coordinate& e,
vector<vector<Color>>* maze_ptr,
vector<Coordinate>* path_ptr) {
auto& maze = *maze_ptr;
// Checks cur is within maze and is a white pixel.
if (cur.x < 0 || cur.x >= size(maze) || cur.y < 0 ||
cur.y >= size(maze[cur.x]) || maze[cur.x][cur.y] != Color::kWhite) {
return false;
}
auto& path = *path_ptr;
path.emplace_back(cur);
maze[cur.x][cur.y] = Color::kBlack;
if (cur == e) {
return true;
}
for (const Coordinate& next_move : vector<Coordinate>{{cur.x, cur.y + 1},
{cur.x, cur.y - 1},
{cur.x + 1, cur.y},
{cur.x - 1, cur.y}}) {
if (SearchMazeHelper(next_move, e, maze_ptr, path_ptr)) {
return true;
}
}
// Cannot find a path, remove the entry added in path.emplace_back(cur).
path.pop_back();
return false;
}
namespace test_framework {
template <>
struct SerializationTrait<Color> : SerializationTrait<int> {
using serialization_type = Color;
static serialization_type Parse(const json& json_object) {
return static_cast<serialization_type>(
SerializationTrait<int>::Parse(json_object));
}
};
} // namespace test_framework
namespace test_framework {
template <>
struct SerializationTrait<Coordinate> : UserSerTrait<Coordinate, int, int> {
static std::vector<std::string> GetMetricNames(const std::string& arg_name) {
return {};
}
static std::vector<int> GetMetrics(const Coordinate& x) { return {}; }
};
} // namespace test_framework
bool PathElementIsFeasible(const vector<vector<Color>>& maze,
const Coordinate& prev, const Coordinate& cur) {
if (!(0 <= cur.x && cur.x < maze.size() && 0 <= cur.y &&
cur.y < maze[cur.x].size() && maze[cur.x][cur.y] == Color::kWhite)) {
return false;
}
return cur == Coordinate{prev.x + 1, prev.y} ||
cur == Coordinate{prev.x - 1, prev.y} ||
cur == Coordinate{prev.x, prev.y + 1} ||
cur == Coordinate{prev.x, prev.y - 1};
}
bool SearchMazeWrapper(TimedExecutor& executor,
const vector<vector<Color>>& maze, const Coordinate& s,
const Coordinate& e) {
vector<vector<Color>> copy = maze;
auto path = executor.Run([&] { return SearchMaze(copy, s, e); });
if (path.empty()) {
return s == e;
}
if (!(path.front() == s) || !(path.back() == e)) {
throw TestFailure("Path doesn't lay between start and end points");
}
for (size_t i = 1; i < path.size(); i++) {
if (!PathElementIsFeasible(maze, path[i - 1], path[i])) {
throw TestFailure("Path contains invalid segments");
}
}
return true;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"executor", "maze", "s", "e"};
return GenericTestMain(args, "search_maze.cc", "search_maze.tsv",
&SearchMazeWrapper, DefaultComparator{}, param_names);
}