forked from MahaKoala/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 3
/
9.7-recursionAndDynamicProgramming.cxx
176 lines (153 loc) · 6.39 KB
/
9.7-recursionAndDynamicProgramming.cxx
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
/*
* 9.7 Implement the "paint fill" function that one might see on many
* image editing programs. That is, given a screen (represented by a
* two-dimensional array of colors), a point, and a new color, fill
* in the surrounding area until the color changes from the original color.
*/
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <unistd.h>
#include "assert.h"
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned char uchar;
struct Bounds {
Bounds(ushort l, ushort r, ushort b, ushort t):
left(l),
right(r),
bottom(b),
top(t) {
}
ushort left;
ushort right;
ushort bottom;
ushort top;
};
template<class T>
class Image {
public:
Image(ushort w, ushort h) {
width = w;
height = h;
pixels = 0;
}
~Image() {
for (size_t i = 0; i < width; i++)
free(pixels[i]);
pixels = 0;
}
void paintFill(ushort pX, ushort pY, T colour, T threshold, Bounds b) {
Image<bool> painted(width, height);
painted.alloc();
painted.fill(false, b);
paintFillR(pX, pY, colour, pixels[pX][pY], threshold, b, painted);
}
void alloc() {
pixels = (T**)malloc(width * sizeof(T*));
for (size_t i = 0; i < width; i++)
pixels[i] = (T*)malloc(height * sizeof(T));
}
void paintFillR(ushort pX, ushort pY, T colour, T initialColour, T threshold, Bounds& b, Image<bool>& painted) {
assert(pX < painted.width && pY < painted.height);
T diff = abs(pixels[pX][pY] - initialColour);
bool p = painted.pixels[pX][pY];
if (!p && diff <= threshold) {
pixels[pX][pY] = colour;
painted.pixels[pX][pY] = true;
if (pX > b.left && pX > 0)
paintFillR(pX - 1, pY, colour, initialColour, threshold, b, painted);
if (pY < b.top && pY < height - 1)
paintFillR(pX, pY + 1, colour, initialColour, threshold, b, painted);
if (pX < b.right && pX < width - 1)
paintFillR(pX + 1, pY, colour, initialColour, threshold, b, painted);
if (pY > b.bottom && pY > 0)
paintFillR(pX, pY - 1, colour, initialColour, threshold, b, painted);
}
}
void fill(T value, Bounds b) {
for (size_t i = b.left; i < b.right; i++)
for (size_t j = b.bottom; j < b.top; j++)
pixels[i][j] = value;
}
void render(Bounds b) {
for (int i = b.top - 1; i >= b.bottom; i--) {
for (int j = b.left; j < b.right; j++) {
uint numVal = (int)pixels[j][i];
numVal ? printf("%d ", numVal) : printf(" ");
}
printf("\n");
}
}
ushort width;
ushort height;
T** pixels;
bool isAllocated;
};
void test9_7() {
// create an 8 bit monochromatic image, that's 30 pixels wide, and 15 pixels high
int width = 30;
int height = 15;
Image<ushort> img(width, height);
img.alloc();
// paint it black
img.fill(0, Bounds(0, width, 0, height));
// draw two slightly overlapping rectangles, with values 5 and 6
img.fill(5, Bounds(width * 0.1, width * 0.7, height * 0.1, height * 0.7));
img.fill(6, Bounds(width * 0.4, width * 0.8, height * 0.5, height * 0.8));
// img.render(Bounds(0, width, 0, height));
//
//
//
// 6 6 6 6 6 6 6 6 6 6 6 6
// 6 6 6 6 6 6 6 6 6 6 6 6
// 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6
// 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6
// 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
//
// paintfill the outside with a value of 1, and a threshold of 0
img.paintFill(0, 0, 1, 1, Bounds(0, width, 0, height));
// img.render(Bounds(0, width, 0, height));
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 6 6 6 6 6 6 6 6 6 6 6 6 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 6 6 6 6 6 6 6 6 6 6 6 6 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1
// 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// paint the colour 6 black, with a threshold of 1
img.paintFill(width * 0.5, height * 0.6, 0, 1, Bounds(0, width, 0, height));
//img.render(Bounds(0, width, 0, height));
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
assert(img.pixels[0][0] == 1 && img.pixels[4][2] == 0);
printf("9.7 passed!\n");
}