-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.h
More file actions
210 lines (175 loc) · 3.69 KB
/
Maze.h
File metadata and controls
210 lines (175 loc) · 3.69 KB
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
#define _CRT_SECURE_NO_DEPRECATE
#pragma once
#include<iostream>
#include<assert.h>
#include<stack>
using namespace std;
/* //在 文件中的格式
8 9
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1
1 1 0 1 0 0 0 0 1
1 1 0 1 0 1 1 0 1
1 1 0 0 0 0 0 0 1
1 1 0 1 1 0 1 0 1
*/
void InitMaze(int ** maze, FILE * fp, int row, int col) //初始化迷宫
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col;)
{
char ch = fgetc(fp);
if (ch == '1' || ch == '0')
{
maze[i][j] = ch - '0';
j++;
}
}
}
}
bool CheckAccess(int **maze, int row, int col, Pos cur, Pos next) //检查next这个位置是否可通
{
if (next._row >= 0 && next._row < row
&& next._col >= 0 && next._col < col) //判断是否在迷宫坐标范围内,以防越界
{
if (maze[cur._row][cur._col] < maze[next._row][next._col] || maze[next._row][next._col] == 0)
{
return true;
}
}
return false;
}
bool GetMazePath(int **maze,int row,int col, stack<Pos>& path,stack<Pos> & shortPath,Pos cur)
{
path.push(cur);
if (cur._row == row - 1)
{
if (shortPath.empty() || shortPath.size() > path.size()) //获取最短路径
{
shortPath = path;
}
}
Pos next = cur;
//上
next._row--;
if (CheckAccess(maze,row,col,cur,next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1; //将走过的路进行标记,标记方法为走过的路依次递增;
if (GetMazePath(maze,row,col,path,shortPath,next))
{
return true;
}
}
//右
next = cur;
next._col++;
if (CheckAccess(maze, row, col, cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
if (GetMazePath(maze, row, col, path, shortPath, next))
{
return true;
}
}
//下
next = cur;
next._row++;
if (CheckAccess(maze, row, col, cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
if (GetMazePath(maze, row, col, path, shortPath, next))
{
return true;
}
}
//左
next = cur;
next._col--;
if (CheckAccess(maze, row, col, cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
if (GetMazePath(maze, row, col, path, shortPath, next))
{
return true;
}
}
path.pop();
return false;
}
void ReleaseMaze(int **maze,int row) //释放空间
{
for (int i = 0; i < row; i++)
{
delete[] maze[i];
maze[i] = NULL;
}
delete [] maze;
maze = NULL;
}
void DisPlay(int **maze, int row, int col) //打印迷宫
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col;j++)
{
cout << maze[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void PrintShortPath(stack<Pos> & cur, int row) //打印最短路径
{
if (cur.empty())
{
return;
}
Pos tmp(cur.top());
cur.pop();
PrintShortPath(cur,row);
if (tmp._row == row - 1) //打印路径
{
cout << "(" << tmp._row << "," << tmp._col << ")" << endl;;
}
else
{
cout << "(" << tmp._row << "," << tmp._col << ")" << "-->";
}
}
void TestMazePath()
{
int **maze = NULL; //c++法动态开辟二维数组
FILE * file = fopen("MazeMap.txt", "r");
assert(file);
int a[2]; //a[0]行;a[1] 列;
for (int i = 0; i < 2; i++)
{
fscanf(file,"%d",&a[i]); //获取行列值;
}
cout << a[0] << " " << a[1] << endl;
char ch = fgetc(file);
while (ch != '\n') //清除第一行剩余字符;
;
//动态开辟二维数组;
maze = new int *[a[0]];
for (int i = 0; i < a[0]; i++)
{
maze[i] = new int[a[1]];
}
InitMaze(maze,file,a[0],a[1]); //初始化二维数组;
DisPlay(maze,a[0],a[1]);
//取起始位置
Pos entry;
entry._row = 2;
entry._col = 0;
stack<Pos> path; //所有路径
stack<Pos> shortPath; //最短路径
GetMazePath(maze,a[0], a[1], path, shortPath, entry); //获得最短路径
DisPlay(maze, a[0],a[1]);
cout << "最短路径:" << endl;
PrintShortPath(shortPath, a[0]);
ReleaseMaze(maze, a[0]); //new在堆上开辟空间,必须delete释放,不然出现内存泄漏;
}