通过邻接表实现广度优先搜索(迷宫问题)

最后一次更新时间:Friday, April 3rd 2020, PM

 

“Talk is cheap. Show me the code.” — Linus Torvalds

直接上代码,如下

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
/***************************************
问题:迷宫问题
算法:广度优先搜索
数据结构:邻接表
***************************************/
#include <stdio.h>
#include <stdlib.h>
#define R 7

typedef struct _Branch
{
int row;
int column;

_Branch * nextBranch;
}
* Branch;

typedef struct _Root
{
int row;
int column;

_Root * nextRoot;
_Branch * nextBranch;
}
* Root;

/////////////////////////////全局变量
int map[R][R];//迷宫
Root treeRoot;//树根
int level = 1;//第几层
///////////////////////

void printMap()
{
printf("\n---------------------------------------------------------------------\n");

int i = 0;
int j = 0;

for (i = 0; i < R; i++)
{
for (j = 0; j < R; j++)
{
if (map[i][j] == -1)
{
printf("\t□");
}
else
{
printf("\t%d", map[i][j]);
}
}
printf("\n");
}
}

Branch findBranch(Root tempRoot, int templevel)//寻路
{
Branch tempBranch = (struct _Branch *) malloc(sizeof(struct _Branch));
int tempRow = tempRoot -> row;
int tempColumn = tempRoot -> column;

//up
if (map[tempRow - 1][tempColumn] == 0)
{
tempBranch -> row = tempRow - 1;
tempBranch -> column = tempColumn;
map[tempRow - 1][tempColumn] = templevel + 1;//等于当前的层数加一

return tempBranch;
}
else if (map[tempRow - 1][tempColumn] == 9)//出口是9
{
level = 0;//找到了出口,把当前层数变成0,并返回NULL
}

//right
if (map[tempRow][tempColumn + 1] == 0)
{
tempBranch -> row = tempRow;
tempBranch -> column = tempColumn + 1;
map[tempRow][tempColumn + 1] = templevel + 1;

return tempBranch;
}
else if (map[tempRow][tempColumn + 1] == 9)
{
level = 0;
}

//down
if (map[tempRow + 1][tempColumn] == 0)
{
tempBranch -> row = tempRow + 1;
tempBranch -> column = tempColumn;
map[tempRow + 1][tempColumn] = templevel + 1;

return tempBranch;
}
else if (map[tempRow + 1][tempColumn] == 9)
{
level = 0;
}

//left
if (map[tempRow][tempColumn - 1] == 0)
{
tempBranch -> row = tempRow;
tempBranch -> column = tempColumn - 1;
map[tempRow][tempColumn - 1] = templevel + 1;

return tempBranch;
}
else if (map[tempRow][tempColumn - 1] == 9)
{
level = 0;
}
return NULL;
}

void addRoot(Branch tempBranch)
{
Root tempRoot = (struct _Root *) malloc (sizeof(struct _Root));
Root tempBranchRoot = (struct _Root *) malloc (sizeof(struct _Root));
//初始化
{
tempBranchRoot -> row = tempBranch -> row;
tempBranchRoot -> column = tempBranch -> column;
tempBranchRoot -> nextBranch = NULL;
tempBranchRoot -> nextRoot = NULL;
}

tempRoot = treeRoot;
while (tempRoot -> nextRoot != NULL)
{
tempRoot = tempRoot -> nextRoot;
}
tempRoot -> nextRoot = tempBranchRoot;
}

void createRoot(Root tempRoot)
{
Branch tempBranch = (struct _Branch *) malloc(sizeof(struct _Branch));
tempBranch = tempRoot -> nextBranch;

while (tempBranch != NULL)
{
addRoot(tempBranch);//把当前branch变成root
tempBranch = tempBranch -> nextBranch;
}
}

void createTree()
{
Root tempRoot = (struct _Root *) malloc (sizeof(struct _Root));
Branch tempBranch = (struct _Branch *) malloc(sizeof(struct _Branch));
Branch lastBranch = (struct _Branch *) malloc(sizeof(struct _Branch));

tempRoot = treeRoot;

while (level != 0)//标识符,如果为0就代表找到了出口
{
tempBranch = findBranch(tempRoot, map[tempRoot -> row][tempRoot -> column]);
//为当前root建立branch
while (tempBranch != NULL)//如果还有branch
{
tempBranch -> nextBranch = NULL;
tempRoot -> nextBranch = tempBranch;
lastBranch = tempBranch;

tempBranch = findBranch(tempRoot, map[tempRoot -> row][tempRoot -> column]);

while (tempBranch != NULL)
{
lastBranch -> nextBranch = tempBranch;
lastBranch = tempBranch;
tempBranch = findBranch(tempRoot, map[tempRoot -> row][tempRoot -> column]);
}
}
createRoot(tempRoot);//把当前root的branch创建成root,放在表尾
tempRoot = tempRoot -> nextRoot;//接着寻找下一个root

printMap();
}
}

int main(void)

{
map[0][0] = -1; map[0][1] = -1; map[0][2] = -1; map[0][3] = -1; map[0][4] = -1; map[0][5] = -1; map[0][6] = -1;
map[1][0] = -1; map[1][1] = 1; map[1][2] = 0; map[1][3] = 0; map[1][4] = 0; map[1][5] = 0; map[1][6] = -1;
map[2][0] = -1; map[2][1] = -1; map[2][2] = -1; map[2][3] = 0; map[2][4] = -1; map[2][5] = -1; map[2][6] = -1;
map[3][0] = -1; map[3][1] = -1; map[3][2] = -1; map[3][3] = 0; map[3][4] = -1; map[3][5] = -1; map[3][6] = -1;
map[4][0] = -1; map[4][1] = 0; map[4][2] = 0; map[4][3] = 0; map[4][4] = 0; map[4][5] = 0; map[4][6] = -1;
map[5][0] = -1; map[5][1] = -1; map[5][2] = -1; map[5][3] = 0; map[5][4] = -1; map[5][5] = 9; map[5][6] = -1;
map[6][0] = -1; map[6][1] = -1; map[6][2] = -1; map[6][3] = -1; map[6][4] = -1; map[6][5] = -1; map[6][6] = -1;
printMap();

//初始化根
{
treeRoot = (struct _Root *) malloc (sizeof(struct _Root));
treeRoot -> nextRoot = NULL;
treeRoot -> row = 1;
treeRoot -> column = 1;
}

createTree();

return 0;
}