-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
matrix_connected_regions.cc
69 lines (60 loc) · 1.9 KB
/
matrix_connected_regions.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
#include <deque>
#include <initializer_list>
#include <queue>
#include <utility>
#include <vector>
#include "test_framework/generic_test.h"
#include "test_framework/timed_executor.h"
using std::deque;
using std::initializer_list;
using std::pair;
using std::queue;
using std::vector;
void FlipColor(int x, int y, vector<deque<bool>>* image_ptr) {
vector<deque<bool>>& image = *image_ptr;
const bool color = image[x][y];
queue<pair<int, int>> q;
image[x][y] = !color; // Flips.
q.emplace(x, y);
while (!empty(q)) {
const auto [x, y] = q.front();
q.pop();
for (const auto& [next_x, next_y] : vector<pair<int, int>>{
{x, y + 1}, {x, y - 1}, {x + 1, y}, {x - 1, y}}) {
if (next_x >= 0 && next_x < size(image) && next_y >= 0 &&
next_y < size(image[next_x]) && image[next_x][next_y] == color) {
// Flips the color.
image[next_x][next_y] = !color;
q.emplace(next_x, next_y);
}
}
}
}
vector<vector<int>> FlipColorWrapper(TimedExecutor& executor, int x, int y,
vector<vector<int>> image) {
vector<deque<bool>> b;
b.reserve(image.size());
for (const vector<int>& row : image) {
deque<bool> tmp;
tmp.resize(row.size());
for (int i = 0; i < row.size(); ++i) {
tmp[i] = static_cast<bool>(row[i]);
}
b.push_back(tmp);
}
executor.Run([&] { FlipColor(x, y, &b); });
image.resize(b.size());
for (int i = 0; i < image.size(); ++i) {
image[i].resize(b.size());
for (int j = 0; j < image[i].size(); ++j) {
image[i][j] = b[i][j];
}
}
return image;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"executor", "x", "y", "image"};
return GenericTestMain(args, "matrix_connected_regions.cc", "painting.tsv",
&FlipColorWrapper, DefaultComparator{}, param_names);
}