forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flood_fill.c
253 lines (197 loc) · 5.4 KB
/
flood_fill.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct canvas {
int max_x, max_y;
int *data;
};
struct point {
int x, y;
};
struct stack {
size_t top, capacity;
struct point *data;
};
struct queue {
size_t front, back, capacity;
struct point *data;
};
int inbounds(struct point p, struct canvas c) {
return (p.x < 0 || p.y < 0 || p.y >= c.max_y || p.x >= c.max_x) ? 0 : 1;
}
int find_neighbors(struct canvas c, struct point p, int old_val,
struct point *neighbors) {
int cnt = 0;
struct point points[4] = {
{p.x, p.y + 1},
{p.x + 1, p.y},
{p.x, p.y - 1},
{p.x - 1, p.y}
};
for (int i = 0; i < 4; ++i) {
if (inbounds(points[i], c) &&
c.data[points[i].x + c.max_x * points[i].y] == old_val) {
neighbors[cnt++] = points[i];
}
}
return cnt;
}
struct stack get_stack() {
struct stack stk;
stk.data = malloc(4 * sizeof(struct point));
stk.capacity = 4;
stk.top = 0;
return stk;
}
int stack_empty(struct stack stk) {
return stk.top == 0;
}
void stack_push(struct stack *stk, struct point element) {
if (stk->top == stk->capacity) {
stk->capacity *= 2;
stk->data = realloc(stk->data, stk->capacity * sizeof(stk->data[0]));
}
stk->data[stk->top++] = element;
}
struct point stack_pop(struct stack *stk) {
return stk->data[--stk->top];
}
void free_stack(struct stack stk) {
free(stk.data);
}
void stack_fill(struct canvas c, struct point p, int old_val, int new_val) {
if (old_val == new_val) {
return;
}
struct stack stk = get_stack();
stack_push(&stk, p);
while (!stack_empty(stk)) {
struct point cur_loc = stack_pop(&stk);
if (c.data[cur_loc.x + c.max_x * cur_loc.y] == old_val) {
c.data[cur_loc.x + c.max_x * cur_loc.y] = new_val;
struct point neighbors[4];
int cnt = find_neighbors(c, cur_loc, old_val, neighbors);
for (int i = 0; i < cnt; ++i) {
stack_push(&stk, neighbors[i]);
}
}
}
free_stack(stk);
}
struct queue get_queue() {
struct queue q;
q.data = calloc(4, sizeof(struct point));
q.front = 0;
q.back = 0;
q.capacity = 4;
return q;
}
int queue_empty(struct queue q) {
return q.front == q.back;
}
void enqueue(struct queue *q, struct point element) {
if (q->front == (q->back + 1) % q->capacity) {
size_t size = sizeof(q->data[0]);
struct point *tmp = calloc((q->capacity * 2), size);
memcpy(tmp, q->data + q->front, (q->capacity - q->front) * size);
memcpy(tmp + q->capacity - q->front, q->data, (q->front - 1) * size);
free(q->data);
q->data = tmp;
q->back = q->capacity - 1;
q->front = 0;
q->capacity *= 2;
}
q->data[q->back] = element;
q->back = (q->back + 1) % q->capacity;
}
struct point dequeue(struct queue *q) {
struct point ret = q->data[q->front];
q->front = (q->front + 1) % q->capacity;
return ret;
}
void free_queue(struct queue q) {
free(q.data);
}
void queue_fill(struct canvas c, struct point p, int old_val, int new_val) {
if (old_val == new_val) {
return;
}
struct queue q = get_queue(sizeof(struct point *));
enqueue(&q, p);
while (!queue_empty(q)) {
struct point cur_loc = dequeue(&q);
if (c.data[cur_loc.x + c.max_x * cur_loc.y] == old_val) {
c.data[cur_loc.x + c.max_x * cur_loc.y] = new_val;
struct point neighbors[4];
int cnt = find_neighbors(c, cur_loc, old_val, neighbors);
for (int i = 0; i < cnt; ++i) {
enqueue(&q, neighbors[i]);
}
}
}
free_queue(q);
}
void recursive_fill(struct canvas c, struct point p, int old_val,
int new_val) {
if (old_val == new_val) {
return;
}
c.data[p.x + c.max_x * p.y] = new_val;
struct point neighbors[4];
int cnt = find_neighbors(c, p, old_val, neighbors);
for (int i = 0; i < cnt; ++i) {
recursive_fill(c, neighbors[i], old_val, new_val);
}
}
int grid_cmp(int *a, int *b, int size) {
for (int i = 0; i < size; ++i) {
if (a[i] != b[i]) {
return 0;
}
}
return 1;
}
int main() {
int grid[25] = {
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 1, 1, 1, 1,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
};
int grid1[25] = {
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 1, 1, 1, 1,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
};
int grid2[25] = {
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 1, 1, 1, 1,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
};
int answer_grid[25] = {
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
};
struct canvas c = {5, 5, grid};
struct canvas c1 = {5, 5, grid1};
struct canvas c2 = {5, 5, grid2};
struct point start_loc = {0, 0};
int pass_cnt = 0;
recursive_fill(c, start_loc, 0, 1);
pass_cnt += grid_cmp(grid, answer_grid, 25);
stack_fill(c1, start_loc, 0, 1);
pass_cnt += grid_cmp(grid1, answer_grid, 25);
queue_fill(c2, start_loc, 0, 1);
pass_cnt += grid_cmp(grid2, answer_grid, 25);
printf("Test Summary: | Pass\tTotal\n");
printf("Fill Methods |\t%d\t3\n", pass_cnt);
return 0;
}