-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.cpp
42 lines (35 loc) · 967 Bytes
/
color.cpp
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
#include <vector>
#include <stack>
#include <iostream>
#include "color.h"
using namespace std;
using namespace emboarden;
int emboarden::color_img(vector<int> &img, int width) {
int next_color = 0;
int height = img.size()/width;
for (unsigned i = 0; i < img.size(); ++i)
if (img[i] != -1) img[i] = -2;
for (unsigned i = 0; i < img.size(); ++i) {
if (img[i] == -2) {
stack<int> s;
s.push(i);
while (!s.empty()) {
int pt = s.top(), x = pt%width, y = pt/width;
s.pop();
if (pt < 0 || pt >= img.size()) {
cout << "Invalid index (" << x << ',' << y << ')' << endl;
break;
}
if (img[pt] == -2) {
if (x > 0) s.push(pt - 1);
if (x < width - 1) s.push(pt + 1);
if (y > 0) s.push(pt - width);
if (y < height - 1) s.push(pt + width);
img[pt] = next_color;
}
}
++next_color;
}
}
return next_color;
}