-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
390 lines (328 loc) · 10.1 KB
/
main.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <stdio.h>
#include <stdlib.h>
#include "MazeParams.h"
#include "Recursion.h"
int maze[MATRIX_SIZE][MATRIX_SIZE];
#define WALL (1 << 30)
#define MARK (1 << 29)
#define MASK (((-1) ^ WALL) ^ MARK)
int start_col; // the starting column for the maze;
void printMaze(void) {
unsigned r, c;
for (r = 0; r < MATRIX_SIZE; r += 1) {
for (c = 0; c < MATRIX_SIZE; c += 1) {
switch (maze[r][c] ) {
case 0:
putchar(' ');
break;
case 1:
putchar('#');
break;
case 2: // bread crumb
putchar('o');
break;
default: // error!
putchar('@');
break;
}
}
putchar('\n');
}
}
void printCodedMaze(void) {
unsigned r, c;
for (r = 0; r < MATRIX_SIZE; r += 1) {
for (c = 0; c < MATRIX_SIZE; c += 1) {
switch(maze[r][c] & WALL) {
case WALL:
putchar('#');
break;
case 0:
putchar(' ');
break;
}
}
putchar('\n');
}
}
enum Directions {
UP,
DOWN,
LEFT,
RIGHT
};
void interpretDir(int* prow, int* pcol, int dir) {
switch (dir) {
case UP: *prow = *prow - 1; break;
case DOWN: *prow = *prow + 1; break;
case LEFT: *pcol = *pcol - 1; break;
case RIGHT: *pcol = *pcol + 1; break;
}
}
void clearWall(int row, int col, int dir) {
int r = row * 2 + 1;
int c = col * 2 + 1;
interpretDir(&r, &c, dir);
maze[r][c] &= ~WALL;
}
void markCell(int row, int col) {
int r = row * 2 + 1;
int c = col * 2 + 1;
maze[r][c] |= MARK;
}
int isMarked(int row, int col) {
int r = row * 2 + 1;
int c = col * 2 + 1;
return (maze[r][c] & MARK);
}
/*
* return an integer that uniquely identifies each cell
*/
int cellID(int row, int col) {
int r = row * 2 + 1;
int c = col * 2 + 1;
return r * MATRIX_SIZE + c;
}
/*
* reverse the cellID to compute the row and col numbers
*/
void findCell(int cell, int* prow, int* pcol) {
int r = cell / MATRIX_SIZE;
int c = cell % MATRIX_SIZE;
*prow = r / 2;
*pcol = c / 2;
}
/*
* store some annotation information in a cell
* (the annotation must be an int less than 25 bits (i.e., smaller than 32 million)
*/
void annotateCell(int row, int col, int annotation) {
int r = row * 2 + 1;
int c = col * 2 + 1;
maze[r][c] &= ~MASK;
maze[r][c] |= annotation;
}
int annotation(int row, int col) {
int r = row * 2 + 1;
int c = col * 2 + 1;
return maze[r][c] & MASK;
}
void clearCellMark(int row, int col) {
int r = row * 2 + 1;
int c = col * 2 + 1;
maze[r][c] &= ~MARK;
}
void clearCell(int row, int col) {
int r = row * 2 + 1;
int c = col * 2 + 1;
maze[r][c] = 0;
}
void makeAllWalls(void) {
unsigned r, c;
unsigned row, col;
for (r = 0; r < MATRIX_SIZE; r += 1) {
for (c = 0; c < MATRIX_SIZE; c += 1) {
maze[r][c] = WALL;
}
}
for (row = 0; row < MAZE_SIZE; row += 1) {
for (col = 0; col < MAZE_SIZE; col += 1) {
clearCell(row, col);
}
}
}
/*
* this function makes a random maze with exactly one path between
* any two points in the maze
*
* If you're curious about the algorithm, come talk to me. It's not very
* complicated (although the code might be confusing)
*/
void makeMaze() {
int num_visited = 1;
int first;
int finish_col;
makeAllWalls();
markCell(0, 0); // mark the first cell as visited
/* add the first cell (row 0, col 0) to the linked list of visited cells */
first = cellID(0, 0);
annotateCell(0, 0, 0);
while(num_visited < MAZE_SIZE * MAZE_SIZE) {
int visit = rand() % num_visited;
int cell = first;
int row, col;
int d;
int nrow, ncol;
findCell(cell, &row, &col);
while (visit > 0) {
cell = annotation(row, col);
findCell(cell, &row, &col);
visit -= 1;
}
d = rand() % 4;
nrow = row; // row of neighbor cell
ncol = col; // col of neighbor cell
interpretDir(&nrow, &ncol, d);
if (nrow >= 0 && nrow < MAZE_SIZE
&& ncol >= 0 && ncol < MAZE_SIZE
&& !isMarked(nrow, ncol)) {
clearWall(row, col, d);
num_visited += 1;
markCell(nrow, ncol);
annotateCell(nrow, ncol, first);
first = cellID(nrow, ncol);
}
}
start_col = rand() % MAZE_SIZE;
start_col = 2 * start_col + 1;
maze[0][start_col] &= ~WALL;
finish_col = rand() % MAZE_SIZE;
maze[MATRIX_SIZE - 1][2 * finish_col + 1] &= ~WALL;
}
/*
* recode the maze so that all walls are exactly 1 and all
* other cells are exactly zero
*/
void recodeMaze(void) {
int r, c;
for (r = 0; r < MATRIX_SIZE; r += 1) {
for (c = 0; c < MATRIX_SIZE; c += 1) {
maze[r][c] = ((maze[r][c] & WALL) == WALL);
}
}
}
int main(void) {
const int magic_number = 13017;
/*test min*/
int a[] = {444, 641, 6782, 4563, 444, 2355, 3456, 767, 2438, 9532, 1340, 325423, 444};
int len = sizeof(a)/sizeof(a[0]);
//int a[] = {-8, 0, -8};
//printf("%d\n", sizeof(a)/sizeof(a[0]));
printf("the smallest of the first 10 natural numbers is: %d\n", minIt(a, len));
printf("the smallest of the first 10 natural numbers is: %d\n", minRec1(a, len));
printf("the smallest of the first 10 natural numbers is: %d\n", minRec2(a, len));
printf("now the smallest is %d\n", minIt(a, len));
printf("now the smallest is %d\n", minRec1(a, len));
printf("now the smallest is %d\n", minRec2(a, len));
/*test sqrt*/
/* printf("the sqrt of 25 is %.16g\n", sqrtIt(25.0, 0, 25.0));
printf("the sqrt of 26 is %.16g\n", sqrtIt(26.0, 0, 26.0));
printf("the sqrt of 2 is %.16g\n", sqrtIt(2.0, 0, 2.0));
printf("the sqrt of 8 is %.16g\n", sqrtIt(8.0, 2.5, 3.0));*/
printf("the sqrt of 25 is %.16g\n", sqrtRec(25.0, 0, 25.0));
printf("the sqrt of 26 is %.16g\n", sqrtRec(26.0, 0, 26.0));
printf("the sqrt of 2 is %.16g\n", sqrtRec(2.0, 0, 2.0));
printf("the sqrt of 8 is %.16g\n", sqrtRec(8.0, 2.5, 3.0));
printf("the sqrt of 4 is %.16g\n", sqrtRec(4.0, 1.999999, 2.11111));
/*test strCompare*/
char* s1; char* s2;
s1 = "apple"; s2 = "apricot";
if (strCompare(s1, s2) < 0) {
printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
}
s1 = "Apple"; s2 = "apple";
if (strCompare(s1, s2) < 0) {
printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
}
s1 = "baby boy"; s2 = "banana";
if (strCompare(s1, s2) < 0) {
printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
}
s1 = "a rather silly string"; s2 = "a rather silly string";
if (strCompare(s1, s2) == 0) {
printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
}
s1 = "12345"; s2 = "12345";
if (strCompare(s1, s2) == 0) {
printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
}
s1 = "Numbers: 12345"; s2 = "12345";
if (strCompare(s1, s2) > 0) {
printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
}
s1 = "Texas"; s2 = "California";
if (strCompare(s1, s2) > 0) {
printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
}
/*test strCompare2*/
s1 = "apple"; s2 = "Apricot";
if (strCompare2(s1, s2) < 0) {
printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
}
s1 = "Batman!"; s2 = "bat man";
if (strCompare2(s1, s2) == 0) {
printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
}
s1 = "OMG, WTF?"; s2 = "oh my goodness, what the heck?";
if (strCompare2(s1, s2) > 0) {
printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
} else {
printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
}
/*test maze*/
srand(magic_number);
makeMaze();
recodeMaze();
printf("recursive solution to the maze 1\n");
solveMazeRec(0, start_col);
printMaze();
printf("\n\n\n");
srand(2);
makeMaze();
recodeMaze();
printf("recursive solution to the maze 2\n");
solveMazeRec(0, start_col);
printMaze();
printf("\n\n\n");
/* printf("iterative solution to the maze\n");
srand(magic_number);
makeMaze();
recodeMaze();
solveMazeIt(0, start_col);
printMaze();*/
/*test Martian*/
Martian change1 = change(15);
printf("change 1 should be 0d, 3n, 0p and is: %dd %dn %dp\n", change1.dodeks, change1.nicks, change1.pennies);
Martian change2 = change(0);
printf("change 2 should be 0d, 0n, 0p and is: %dd %dn %dp\n", change2.dodeks, change2.nicks, change2.pennies);
Martian change3 = change(17);
printf("change 3 should be 1d, 1n, 0p and is: %dd %dn %dp\n", change3.dodeks, change3.nicks, change3.pennies);
Martian change4 = change(25);
printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);
Martian change5 = change(24);
printf("change 5 should be 2d, 0n, 0p and is: %dd %dn %dp\n", change5.dodeks, change5.nicks, change5.pennies);
Martian change6 = change(23);
printf("change 6 should be 1d, 2n, 1p and is: %dd %dn %dp\n", change6.dodeks, change6.nicks, change6.pennies);
Martian change7 = change(22);
printf("change 7 should be 1d, 2n, 0p and is: %dd %dn %dp\n", change7.dodeks, change7.nicks, change7.pennies);
Martian change8 = change(18);
printf("change 8 should be 1d, 1n, 1p and is: %dd %dn %dp\n", change8.dodeks, change8.nicks, change8.pennies);
Martian change9 = change(27);
printf("change 9 should be 1d, 3n, 0p and is: %dd %dn %dp\n", change9.dodeks, change9.nicks, change9.pennies);
Martian change10 = change(50);
printf("change 10 should be 4d, 0n, 2p and is: %dd %dn %dp\n", change10.dodeks, change10.nicks, change10.pennies);
/* A very simple and obvious test of the general form of Martian
* be sure and test your solution more thoroughly!!!! */
Martian change21 = change(25, 5, 12);
printf("change 21 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change21.dodeks, change21.nicks, change21.pennies);
Martian change22 = change(13, 6, 7);
printf("change 22 should be 1d, 0n, 0p and is: %dd %dn %dp\n", change22.dodeks, change22.nicks, change22.pennies);
}