-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.c
172 lines (152 loc) · 2.99 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
#include<stdio.h>
#define ROW 6
#define COL 6
typedef struct Maze{
int maze[ROW][COL];
}Maze;
void MazeInit(Maze* maze)
{
if(maze == NULL)
{
//非法输入
return;
}
int data[ROW][COL] = {
{0 , 0 , 1 , 0 , 0 , 0},
{0 , 0 , 1 , 1 , 1 , 0},
{0 , 0 , 1 , 0 , 1 , 0},
{0 , 1 , 1 , 0 , 1 , 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->maze[i][j] = data[i][j];
}
}
}
void MazePrint(Maze* maze)
{
if(maze == NULL)
{
//非法输入
return;
}
int i = 0;
for(; i<ROW; i++){
int j = 0;
for(; j<COL; j++){
printf("%2d ",maze->maze[i][j]);
}
printf("\n");
}
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
typedef struct Point{
int row;
int col;
}Point;
//走的点
//判断当前点是否可以落脚
int Canstay(Maze* maze,Point cur)
{
//边界外
if(cur.row<0 || cur.row>=ROW || cur.col<0 || cur.col>=COL)
{
return 0;
}
//遇到了墙
//遇到了标记点
//如果遇到了1,表示该路可走
if(maze->maze[cur.row][cur.col] == 1 )
{
return 1;
}
return 0;
}
//标记当前点
void mask(Maze* maze,Point cur)
{
maze->maze[cur.row][cur.col] = 2;
}
//判断当前点是不是出口
int IsExit(Point cur,Point entry)
{
if(cur.row == entry.row && cur.col == entry.col)
{
return 0;
}
if(cur.row == 0 || cur.col == 0 || cur.row == ROW || cur.col == COL)
{
return 1;
}
return 0;
}
//递归实现迷宫问题
void _GetPath(Maze* maze,Point cur,Point entry)
{
//判断当前点是否可以落脚,不可以的话直接返回
if(!Canstay(maze,cur))
{
return;
}
//标记当前点
mask(maze,cur);
//判断当前点是不是出口
// 是出口,直接返回
// 不是出口,继续
//继续顺时针判断当前点的四个方向
if(IsExit(cur,entry))
{
return;
}
//上
Point up;
up.row = cur.row - 1;
up.col = cur.col;
_GetPath(maze,up,entry);
//右
Point right;
right.row = cur.row;
right.col = cur.col + 1;
_GetPath(maze,right,entry);
//下
Point down;
down.row = cur.row + 1;
down.col = cur.col;
_GetPath(maze,down,entry);
//左
Point left;
left.row = cur.row;
left.col = cur.col - 1;
_GetPath(maze,left,entry);
}
void GetPath(Maze* maze,Point entry)
{
if(maze == NULL)
{
//非法输入
return;
}
_GetPath(maze,entry,entry);
}
void Test()
{
Maze maze;
MazeInit(&maze);
MazePrint(&maze);
printf("\n");
Point entry;
entry.row = 0;
entry.col = 2;
GetPath(&maze,entry);
MazePrint(&maze);
}
int main()
{
Test();
return 0;
}