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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
| #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cmath>
#define N 15
#define M 20
class point {
public:
int x, y;
point(int X, int Y) {
x = X;
y = Y;
}
point& point::operator = (const point& p) {
this->x = p.x;
this->y = p.y;
return *this;
}
friend std::ostream& operator << (std::ostream& stream, const point& p);
friend bool operator == (const point& p1, const point& p2);
};
std::ostream& operator << (std::ostream& stream, const point& p) {
stream << "(" << p.x << ", " << p.y << ").";
return stream;
}
bool operator == (const point& p1, const point& p2) {
return p1.x == p2.x && p1.y == p2.y;
}
void showMas(bool mas[N][M]) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
std::cout << mas[i][j] << " ";
}
std::cout << std::endl;
}
}
std::vector<point> getCorner(const bool mas[N][M]) {
int Neighbour = 0;
int sumI, sumJ;
sumI = sumJ = 0;
std::vector<point> vec; // вектор угловых точек
for (int i = 1; i < N - 1; ++i) {
for (int j = 1; j < M - 1; ++j) {
if (mas[i][j] == true)
continue;
else {
if (mas[i - 1][j - 1] == true) {
++Neighbour;
sumI += i - 1;
sumJ += j - 1;
}
if (mas[i - 1][j] == true) {
++Neighbour;
sumI += i - 1;
sumJ += j;
}
if (mas[i - 1][j + 1] == true) {
++Neighbour;
sumI += i - 1;
sumJ += j + 1;
}
if (mas[i][j - 1] == true) {
++Neighbour;
sumI += i;
sumJ += j - 1;
}
if (mas[i][j + 1] == true) {
++Neighbour;
sumI += i;
sumJ += j + 1;
}
if (mas[i + 1][j - 1] == true) {
++Neighbour;
sumI += i + 1;
sumJ += j - 1;
}
if (mas[i + 1][j] == true) {
++Neighbour;
sumI += i + 1;
sumJ += j;
}
if (mas[i + 1][j + 1] == true) {
++Neighbour;
sumI += i + 1;
sumJ += j + 1;
}
if (Neighbour == 5 && ((abs(sumI - 5*i) == 3 && sumJ - 5*j == 0) || (sumI - 5*i == 0 && abs(sumJ - 5*j) == 3)
|| (abs(sumI - 5*i) == 2 && abs(sumI - 5*i) == abs(sumJ - 5*j)))
|| Neighbour == 6 && sumI - 6*i != 0 && sumJ - 6*j != 0
|| Neighbour > 6)
vec.push_back(point(i, j));
Neighbour = 0;
sumI = 0;
sumJ = 0;
}
}
}
return vec;
}
point findFreeField(const point p, std::vector<point>& cur, bool mas[N][M]) {
int i = p.x;
int j = p.y;
if (mas[i - 1][j - 1] == false && find(cur.begin(), cur.end(), point(i - 1, j - 1)) == cur.end()) {
cur.push_back(point(i, j));
--i; --j;
} else if (mas[i - 1][j] == false && find(cur.begin(), cur.end(), point(i - 1, j)) == cur.end()) {
cur.push_back(point(i, j));
--i;
} else if (mas[i - 1][j + 1] == false && find(cur.begin(), cur.end(), point(i - 1, j + 1)) == cur.end()) {
cur.push_back(point(i, j));
--i; ++j;
} else if (mas[i][j - 1] == false && find(cur.begin(), cur.end(), point(i, j - 1)) == cur.end()) {
cur.push_back(point(i, j));
--j;
} else if (mas[i][j + 1] == false && find(cur.begin(), cur.end(), point(i, j + 1)) == cur.end()) {
cur.push_back(point(i, j));
++j;
} else if (mas[i + 1][j - 1] == false && find(cur.begin(), cur.end(), point(i + 1, j - 1)) == cur.end()) {
cur.push_back(point(i, j));
++i; --j;
} else if (mas[i + 1][j] == false && find(cur.begin(), cur.end(), point(i + 1, j)) == cur.end()) {
cur.push_back(point(i, j));
++i;
} else if (mas[i + 1][j + 1] == false && find(cur.begin(), cur.end(), point(i + 1, j + 1)) == cur.end()) {
cur.push_back(point(i, j));
++i; ++j;
}
point t(i, j);
return t;
}
bool isborder(const point& p) {
return p.x == 0 || p.x == N - 1 || p.y == 0 || p.y == M - 1;
}
void fillVector(const std::vector<point> vp, bool mas[N][M]) {
std::vector<point>::const_iterator cit;
for (cit = vp.begin(); cit != vp.end(); ++cit) {
mas[cit->x][cit->y] = true;
}
}
void filling(point& p, bool mas[N][M]) {
std::vector<point> cur; // вектор точек массива, которые мы включаем в текущую цепочку
point curpoint = p; // будет ли работать если использовать конст ссылку р?
std::vector<point>::size_type i = 0; // счётчик откатов вектора
while (1) {
curpoint = findFreeField(p, cur, mas);
if (p == curpoint) { // если свободных точек нет,
if (find(cur.begin(), cur.end(), p) == cur.end()) // обязательная проверка наличия этой точки в векторе
cur.push_back(p);
p = cur[cur.size() - i - 1]; // то переходим обратно
++i;
std::cout << "yes i = " << i << std::endl;
if (i == cur.size() - 1) {
break;
}
}
else {
p = curpoint; // если есть, то переходим на свободную
if (isborder(p)) {
std::cout << "no i = " << i << std::endl;
std::cout << "size = " << cur.size() << std::endl;
cur.clear();
return;
}
}
}
if (i >= cur.size() - 1) {
fillVector(cur, mas);
cur.clear();
return;
}
}
void showVec(const std::vector<point>& vec) {
std::vector<point>::const_iterator cit;
for (cit = vec.begin(); cit != vec.end(); ++cit)
std::cout << *cit << " ";
}
int main()
{
setlocale(0, "");
srand((unsigned)time(NULL));
bool mas[N][M];
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
mas[i][j] = (rand()%4 != 0); // для увеличения числа 1
}
}
showMas(mas);
std::cout << std::endl;
std::vector<point> v;
v = getCorner(mas);
showVec(v);
//std::vector<point>::size_type size = v.size();
/*std::vector<point>::iterator it;
for (it = v.begin(); it != v.end(); ++it) { // с циклом выдаёт ошибку out of range
filling(*it, mas); // исправишь если очень надо
}*/
filling(v[0], mas);
std::cout << std::endl;
showMas(mas);
filling(v[1], mas);
std::cout << std::endl;
showMas(mas);
system("pause>0");
return 0;
} |