-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskyline_bug.c
288 lines (224 loc) · 5.08 KB
/
skyline_bug.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include "lib/helpers.h"
#include "lib/hash.h"
struct Point
{
int x;
int h;
int is_end;
struct ListNode hook;
};
int FuncPointCompare(void *obj1, void *obj2)
{
struct Point *point1 = obj1;
struct Point *point2 = obj2;
if (point1->x != point2->x)
return FuncIntCompare(&point1->x, &point2->x);
return FuncIntCompare(&point1->is_end, &point2->is_end);
}
int FuncPointToInt(void *obj)
{
struct Point *point = obj;
return ((point->x + point->h) * 5381);
}
void PointDump(struct Point *point)
{
printf(" x %d h %d is_end %d\n", point->x, point->h, point->is_end);
}
void FuncPointDump(void *obj)
{
PointDump(obj);
}
void PointInit(struct Point *point, int x, int h, int is_end)
{
point->x = x;
point->h = h;
point->is_end = is_end;
ListNodeInit(&point->hook, point);
}
void PreparePoint(struct Hash *hash, int x, int h, int is_end)
{
struct Point *point = NULL, point_tmp;
PointInit(&point_tmp, x, h, is_end);
if ((point = HashGet(hash, &point_tmp)) == NULL)
{
point = calloc(1, sizeof(struct Point));
PointInit(point, x, h, is_end);
HashInsert(hash, point, point);
}
else if (point->h < h)
point->h = h;
}
void HashDump(struct Hash *hash)
{
struct ListNode *head = &hash->list_nodes, *p = NULL;
for (p = head->next; p != head; p = p->next)
{
struct HashNode *hash_node = p->container;
struct Point *point = hash_node->val;
PointDump(point);
}
}
void PrepareHash(struct Hash *hash, int **buildings, int num_building, int num_building_col)
{
int i = 0;
for (i = 0; i < num_building; ++i)
{
int s = buildings[i][0];
int e = buildings[i][1];
int h = buildings[i][2];
PreparePoint(hash, s, h, 0);
PreparePoint(hash, e, h, 1);
}
HashDump(hash);
printf("\n");
}
struct Point* GenerateByCompare(struct Point *point_last, struct Point *point, int *last_h, struct ListNode *list_results)
{
printf("last");
PointDump(point_last);
printf("curr");
PointDump(point);
if (! point_last->is_end) // s1
{
if (! point->is_end) // s2
{
// struct Point *point_mid = NULL;
printf("%d\n", __LINE__);
if (point_last->h >= point->h) // h1 >= h2
{
*last_h = point->h;
return point_last;
}
/*
printf("%d\n", __LINE__);
point_mid = calloc(1, sizeof(struct Point));
PointInit(point_mid, point->x, point_last->h, 0); // s2, h1
ListAddTail(list_results, &point_mid->hook);
*/ }
// e1
else if (point_last->h > point->h)
{
printf("%d\n", __LINE__);
*last_h = point->h;
return point_last;
}
}
else // e1
{
if (! point->is_end) // s2
{
printf("%d\n", __LINE__);
point_last->h = 0;
}
// e2
else if (point_last->h > point->h)
{
// struct Point *point_mid = NULL;
printf("%d\n", __LINE__);
/*
point_mid = calloc(1, sizeof(struct Point));
PointInit(point_mid, point_last->x, point->h, 0); // e1, h2
ListAddTail(list_results, &point_mid->hook);
*/
if (*last_h > point->h)
{
point_last->h = *last_h;
return point_last;
}
point_last->h = point->h;
*last_h = point->h;
}
else if (point_last->h == point->h)
{
printf("%d\n", __LINE__);
point_last->x = point->x;
point_last->h = point->h;
return point_last;
}
}
printf("%d\n", __LINE__);
ListAddTail(list_results, &point->hook);
return point;
}
void GeneratePoints(struct Hash *hash, struct ListNode *list_results)
{
struct ListNode *head = &hash->list_nodes, *p = NULL;
struct HashNode *hash_node = NULL;
struct Point *point = NULL, *point_last = NULL;
int last_h = 0;
// take first
p = head->next;
hash_node = p->container;
point = hash_node->val;
ListAddTail(list_results, &point->hook);
point_last = point;
last_h = point->h;
// others
p = p->next;
for (; p != head; p = p->next)
{
hash_node = p->container;
point = hash_node->val;
GenerateByCompare(point_last, point, &last_h, list_results);
point_last = point;
}
// last h 0
p = list_results->prev;
point = p->container;
point->h = 0;
}
void DumpList(struct ListNode *list)
{
struct ListNode *head = list, *p = NULL;
printf("\n");
for (p = head->next; p != head; p = p->next)
{
struct Point *point = p->container;
PointDump(point);
}
printf("\n");
}
void GetSkyline(int **buildings, int num_building, int num_building_col)
{
struct Hash hash;
struct ListNode list_results;
HashInit(&hash, FuncPointToInt, FuncPointCompare);
PrepareHash(&hash, buildings, num_building, num_building_col);
HashSort(&hash, SortTypeNonDecreasing, FuncPointDump);
ListInit(&list_results);
GeneratePoints(&hash, &list_results);
DumpList(&list_results);
}
void Test(void)
{
#define kNumRow 5
#define kNumCol 3
int matrix_ref[kNumRow][kNumCol] = {
{2,9,10}
, {3,7,15}
, {5,8,12} // 5,12,12
,
{15,20,10}
, {19,24,8}
// {0, 2, 3}
// , {2, 5, 3}
};
int **matrix = calloc(kNumRow, sizeof(int*));
int i = 0, j = 0;
for (i = 0; i < kNumRow; ++i)
{
matrix[i] = calloc(kNumCol, sizeof(int));
for (j = 0; j < kNumCol; ++j)
matrix[i][j] = matrix_ref[i][j];
}
GetSkyline(matrix, kNumRow, kNumCol);
}
int main(int argc, char *argv[])
{
Test();
return 0;
}