-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.c
487 lines (453 loc) · 10.5 KB
/
maze.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include "maze.h"
#include <stdio.h>
#include "StackQueue.h"
void MazeInit(Maze* maze)
{
if(maze == NULL)
{
return;
}
int arr[ROW][COL] = {
{0,1,0,0,0,0},
{0,1,1,1,0,0},
{0,1,0,1,0,0},
{0,1,1,0,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0}
};
int i = 0;
for(; i < ROW; ++i)
{
int j = 0;
for(; j < COL; ++j)
{
maze->map[i][j] = arr[i][j];
}
}
}
int CanStay(Maze* maze,Point cur)
{
if(cur.row < 0 || cur.row >= ROW || cur.col < 0 || cur.col >= COL)
{
return 0;
}
if(maze->map[cur.row][cur.col] != 1)
{
return 0;
}return 1;
}
void Mark(Maze* maze,Point cur)
{
maze->map[cur.row][cur.col] = 2;
}
int IsExit(Point cur,Point entry)
{
if(cur.row == 0 || cur.row == ROW - 1 || cur.col == 0 || cur.col == COL - 1)
{
if(cur.col == entry.col && cur.row == entry.row)
{
return 0;
}
return 1;
}
return 0;
}
void _HasPath(Maze* maze,Point cur,Point entry)
{
//1.判断当前点是否可以落脚
if(!CanStay(maze,cur))
{
return;
}
//2.标记当前点
Mark(maze,cur);
//3.判断当前点是否是出口(边缘点,不是入口点)
if(IsExit(cur,entry))
{
printf("找到了!!!\n");
}
//4.按一定顺序判断当前点的四周点(递归)
Point up;
up.row = cur.row - 1;
up.col = cur.col;
_HasPath(maze,up,entry);
Point left;
left.row = cur.row;
left.col = cur.col - 1;
_HasPath(maze,left,entry);
Point down;
down.row = cur.row + 1;
down.col = cur.col;
_HasPath(maze,down,entry);
Point right;
right.row = cur.row;
right.col = cur.col + 1;
_HasPath(maze,right,entry);
//5.四周判定完后 返回
return;
}
void HasPath(Maze* maze,Point entry)
{
if(maze == NULL)
{
return;
}
_HasPath(maze,entry,entry);
}
void HasPathByLoop(Maze* maze,Point entry)
{
if(maze == NULL)
{
return;
}
//1.判定入口是否可以落脚
if(!CanStay(maze,entry))
{
return;
}
//2.标记入口点,并将该点入栈
Mark(maze,entry);
SeqStack stack;
SeqStackInit(&stack);
SeqStackPush(&stack,entry);
//3.进入循环
// a)取栈顶元素
Point cur;
while(SeqStackTop(&stack,&cur))
{
// b)判定当前点是不是出口,如果是,就退出
if(IsExit(cur,entry))
{
printf("找到了...\n");
return;
}
// c)按一定顺序判断当前点的四周点
// 若可以落脚,则标记,入栈,进入下一次循环
Point up = cur;
up.row -= 1;
if(CanStay(maze,up))
{
Mark(maze,up);
SeqStackPush(&stack,up);
continue;
}
Point left = cur;
left.col -= 1;
if(CanStay(maze,left))
{
Mark(maze,left);
SeqStackPush(&stack,left);
continue;
}
Point down = cur;
down.row += 1;
if(CanStay(maze,down))
{
Mark(maze,down);
SeqStackPush(&stack,down);
continue;
}
Point right = cur;
right.col += 1;
if(CanStay(maze,right))
{
Mark(maze,right);
SeqStackPush(&stack,right);
continue;
}
// d)若四周都不可以的话,就出栈(回溯)
SeqStackPop(&stack);
}
return;
}
void MazeInitMulExit(Maze* maze)
{
if(maze == NULL)
{
return;
}
int arr[ROW][COL] = {
{0,1,0,0,0,0},
{0,1,1,1,0,0},
{0,1,0,1,1,1},
{1,1,1,0,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0}
};
int i = 0;
for(; i < ROW; ++i)
{
int j = 0;
for(; j < COL; ++j)
{
maze->map[i][j] = arr[i][j];
}
}
}
void _GetShortPath(Maze* maze,Point cur,Point entry,SeqStack* short_path,SeqStack* cur_path)
{
//1.判定当前点是否可以落脚
if(!CanStay(maze,cur))
{
return;
}
//2.标记当前点,把它push到cur_path中
Mark(maze,cur);
SeqStackPush(cur_path,cur);
//3.判定当前点是不是出口
if(IsExit(cur,entry))
{
//如果是出口
//a)比较当前cur_path和short_path的长短
//b)如果cur_path小于short_path(或者short_path为空),那就用cur_path替换short_path
if(SeqStackSize(cur_path) < SeqStackSize(short_path) || SeqStackSize(short_path) == 0)
{
SeqStackAssign(short_path,cur_path);
}
printf("找到了一条当前比较短的路径...\n");
SeqStackDebugPrint(short_path);
//c)让 cur_path 出栈,同时返回到上一层栈桢
SeqStackPop(cur_path);
return;
}
//4.如果当前点不是出口,那就按照一定顺序递归的判定四周的元素(递归)
Point up = cur;
up.row -= 1;
_GetShortPath(maze,up,entry,short_path,cur_path);
Point right = cur;
right.col += 1;
_GetShortPath(maze,right,entry,short_path,cur_path);
Point down = cur;
down.row += 1;
_GetShortPath(maze,down,entry,short_path,cur_path);
Point left = cur;
left.col -= 1;
_GetShortPath(maze,left,entry,short_path,cur_path);
//5.四周元素都判定完应该回溯到上一个位置,先将cur_path出栈,再return
SeqStackPop(cur_path);
return;
}
void GetShrtPath(Maze* maze,Point entry)
{
if(maze == NULL)
{
return;
}
SeqStack short_path;
SeqStackInit(&short_path);
SeqStack cur_path;
SeqStackInit(&cur_path);
_GetShortPath(maze,entry,entry,&short_path,&cur_path);
SeqStackDebugPrint(&short_path);
}
void MazeInitHasCycle(Maze* maze)
{
if(maze == NULL)
{
return;
}
int arr[ROW][COL] = {
{0,1,0,0,0,0},
{0,1,1,1,0,0},
{0,1,0,1,1,1},
{1,1,1,1,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0}
};
int i = 0;
for(; i < ROW; ++i)
{
int j = 0;
for(; j < COL; ++j)
{
maze->map[i][j] = arr[i][j];
}
}
}
int IsValidPoint(Point cur)
{
if(cur.row < 0 || cur.row >= ROW || cur.col < 0 || cur.col >= COL)
{
return 0;
}
return 1;
}
//cur_value 10 pre_value 10 不能走
//cur_value 10 pre_value 9 没有必要走
//cur_value 10 pre_value 8 必须走
//所以 当 cur_value - 1 > pre_value 就必须走
int CanStayWithCycle(Maze* maze,Point cur,Point pre)
{
//1.当前点在不在地图上
//2.当前点的值是 1,可以落脚
if(maze->map[cur.row][cur.col] == 1)
{
return 1;
}
if(IsValidPoint(cur))
{
//3.满足 cur_value - 1 > pre_value
if(pre.row != -1 && pre.col != -1)
{
int pre_value = maze->map[pre.row][pre.col];
int cur_value = maze->map[cur.row][cur.col];
if(cur_value - pre_value > 1)
{
return 1;
}
}
else
{
if(maze->map[cur.row][cur.col] == 1)
{
return 1;
}
}
}
return 0;
}
void MarkWithCycle(Maze* maze,Point pre, Point cur)
{
if(IsValidPoint(pre) == 1)
{
maze->map[cur.row][cur.col] = maze->map[pre.row][pre.col] + 1;
}
else if(IsValidPoint(pre) == 0)
{
maze->map[cur.row][cur.col] = 2;
}
}
void _GetShortPathWithCycle(Maze* maze,Point cur,Point pre, Point entry,SeqStack* cur_path,SeqStack* short_path)
{
//1.判定当前点是否可以落脚(方式变化)
if(CanStayWithCycle(maze,cur,pre) == 0)
{
return;
}
//2.如果可以落脚就标记当前点(方式变化),并且入栈
MarkWithCycle(maze,pre,cur);
SeqStackPush(cur_path,cur);
//3.判定是否是出口
if(IsExit(cur,entry))
{
//a) 判定cur_path 和short_path的大小
//b) 如果cur_path小,就替换short_path
if(SeqStackSize(cur_path) < SeqStackSize(short_path) || SeqStackSize(short_path) == 0)
{
SeqStackAssign(short_path,cur_path);
}
printf("找到一条较短的路径...\n");
SeqStackDebugPrint(short_path);
//c) 出栈,回溯
SeqStackPop(cur_path);
return;
}
//4.如果不是出口,递归判定四个方向
Point up = cur;
up.row -= 1;
_GetShortPathWithCycle(maze,up,cur,entry,cur_path,short_path);
Point right = cur;
right.col += 1;
_GetShortPathWithCycle(maze,right,cur,entry,cur_path,short_path);
Point down = cur;
down.row += 1;
_GetShortPathWithCycle(maze,down,cur,entry,cur_path,short_path);
Point left = cur;
left.col -= 1;
_GetShortPathWithCycle(maze,left,cur,entry,cur_path,short_path);
//5.四个方向都判定完成,出栈,回溯
SeqStackPop(cur_path);
return;
}
void GetShortPathWithCycle(Maze* maze,Point entry)
{
if(maze == NULL)
{
return;
}
SeqStack short_path;
SeqStackInit(&short_path);
SeqStack cur_path;
SeqStackInit(&cur_path);
Point pre = {-1,-1};
_GetShortPathWithCycle(maze,entry,pre,entry,&cur_path,&short_path);
}
////////////////////////////////////////////////////
//测试代码
////////////////////////////////////////////////////
#if 1
#define TEST_HEADER printf("\n----------------%s--------------\n",__FUNCTION__)
void PrintMap(Maze* maze)
{
if(maze == NULL)
{
return;
}
int i = 0;
for(; i < ROW; ++i)
{
int j = 0;
for(; j < COL; ++j)
{
printf("%2d ",maze->map[i][j]);
}
printf("\n");
}
}
void Test()
{
TEST_HEADER;
Maze maze;
MazeInit(&maze);
PrintMap(&maze);
}
void TestHasPath()
{
TEST_HEADER;
Maze maze;
MazeInit(&maze);
Point entry;
entry.row = 0;
entry.col = 1;
HasPath(&maze,entry);
PrintMap(&maze);
}
void TestHasPathByLoop()
{
TEST_HEADER;
Maze maze;
MazeInit(&maze);
Point entry;
entry.row = 0;
entry.col = 1;
HasPathByLoop(&maze,entry);
PrintMap(&maze);
}
void TestMulExit()
{
TEST_HEADER;
Maze maze;
MazeInitMulExit(&maze);
Point entry = {0,1};
GetShrtPath(&maze,entry);
PrintMap(&maze);
}
void TestHasCycleMaze()
{
TEST_HEADER;
Maze maze;
MazeInitHasCycle(&maze);
Point entry = {0,1};
GetShortPathWithCycle(&maze,entry);
PrintMap(&maze);
}
int main()
{
Test();
TestHasPath();
TestHasPathByLoop();
TestMulExit();
TestHasCycleMaze();
return 0;
}
#endif