实验二实验报告


实验目的

  1. 根据注释和已有函数的实现,实现Maze.cpp中的四个缺失函数。使用maze1.txt进行测试。

  2. 修改程序,从记录的路径中移除回溯。确保不添加重复项,如果要添加的位置是倒数第二个,则取路径中的最后一个位置。

  3. 整体进行优化

  • 将迷宫声明为char类型的指针。
  • 修改LoadMaze()以动态分配迷宫的空间。
  • 将路径数组设置为适当大小的动态数组。
  • 在SolveMaze()结束时删除任何分配的内存。 使用maze2.txt和maze3.txt进行测试。

以上步骤,要求我们掌握C++基本语法,文件操作,基本算法思想,内存管理,代码调适与测试等知识点。


实验过程

任务一:

根据要求补全函数

根据Direction的四个方向和二维地图以一维的数组进行存储,我们可以在函数内部,通过对heading进行加1的操作,并使用取模运算符来确保在四个方向(枚举值为0到3)之间循环。这样做的结果是,如果当前方向为DOWN(枚举值为0),则加1后的值为1,表示向右转90度后的方向为LEFT;如果当前方向为LEFT(枚举值为1),则加1后的值为2,表示向右转90度后的方向为UP;以此类推。

代码中的static_cast<Direction>表示对表达式进行类型转换,确保结果值在Direction枚举类型的范围内。

1
2
3
4
5
6
void TurnRight(Direction& heading)
{
// 当前方向向右转90度
// DOWN -> LEFT, LEFT -> UP, UP -> RIGHT, RIGHT -> DOWN
heading = static_cast<Direction>((heading + 1) % 4);
}

同样是根据地图的特点进行更新,根据当前位置和朝向确定当前位置的前方位置,并将当前位置更新为前方位置。

函数接受两个参数:一个是整型引用参数pos,表示当前位置;另一个是Direction类型的参数heading,表示当前朝向。

在函数内部,使用了switch语句根据当前方向来更新位置

  • 如果当前方向为DOWN,则将位置向下移动一格,即pos += mazeWidth,其中mazeWidth是迷宫的宽度。
  • 如果当前方向为LEFT,则将位置向左移动一格,即pos--
  • 如果当前方向为UP,则将位置向上移动一格,即pos -= mazeWidth
  • 如果当前方向为RIGHT,则将位置向右移动一格,即pos++

通过这种方式,根据当前方向更新了位置,实现了前方位置的计算。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 判断前方位置的一个函数
// 根据当前位置和朝向,确定当前位置的前方位置
void MoveForward(int& pos, Direction heading)
{
// to be finished.
// 根据当前方向更新位置
switch (heading)
{
case DOWN:
pos += mazeWidth; // 向下移动一格
break;
case LEFT:
pos--; // 向左移动一格
break;
case UP:
pos -= mazeWidth; // 向上移动一格
break;
case RIGHT:
pos++; // 向右移动一格
break;
}
}

同样是根据地图的特点进行更新,该函数用于确定给定位置和朝向下的前方位置。

函数接受三个参数:一个是整型pos,表示当前位置;一个是Direction类型的参数heading,表示当前朝向;另一个是整型引用参数ahead,用于存储计算得到的前方位置。

在函数内部,首先将ahead初始化为当前位置,即ahead = pos

然后,通过switch语句根据当前朝向来确定前方位置。具体来说:

  • 如果当前方向为DOWN,则将ahead向下移动一格,即ahead += mazeWidth,其中mazeWidth可能是迷宫的宽度。
  • 如果当前方向为LEFT,则将ahead向左移动一格,即ahead--
  • 如果当前方向为UP,则将ahead向上移动一格,即ahead -= mazeWidth
  • 如果当前方向为RIGHT,则将ahead向右移动一格,即ahead++

通过这种方式,确定了给定位置和朝向下的前方位置,并将结果存储在了ahead中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void WheresAhead(int pos, Direction heading, int& ahead)
{
//to be finished.
ahead = pos; // 初始化为当前位置

// 根据当前朝向确定前方位置
switch (heading)
{
case DOWN:
ahead += mazeWidth; // 向下移动一格
break;
case LEFT:
ahead--; // 向左移动一格
break;
case UP:
ahead -= mazeWidth; // 向上移动一格
break;
case RIGHT:
ahead++; // 向右移动一格
break;
}
}

根据方向,我们在函数内部,通过对heading进行加3的操作,并使用取模运算符来确保在四个方向(枚举值为0到3)之间循环。这样做的结果是,如果当前方向为DOWN(枚举值为0),则加3后的值为3,表示向左转90度后的方向为RIGHT;如果当前方向为LEFT(枚举值为1),则加3后的值为0,表示向左转90度后的方向为DOWN;以此类推。

代码中的static_cast<Direction>表示对表达式进行类型转换,确保结果值在Direction枚举类型的范围内。

1
2
3
4
5
6
7
void TurnLeft(Direction& heading)
{
// 当前方向向左转90度
// DOWN -> RIGHT, LEFT -> DOWN, UP -> LEFT, RIGHT -> UP
heading = static_cast<Direction>((heading + 3) % 4);
}

任务二:

删除路径中的回溯,直接记录直达路径。

本做法采取记录重复走过的路径,并删除重复的方式。

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
void SolveMaze()
//解决迷宫问题
{
int pos, other;
Direction heading;
//当前位置,其他位置
//当前的移动方向
FindEntrance(pos);
//找到入口位置,并且存储在pos变量中
heading = DOWN;
//移动方向改为向下
while (!AtExit(pos))
//只要没找到出口,就继续寻找
{
posi[i] = pos;
post[pos]++;
//记录当前到哪个位置
i++;
//进入下一个位置的记录
if (i >= mazeHeight * mazeWidth)
//如果数组超出容量,就退出
{
cout << "array too small\n";
abort();
}
WheresRight(pos, heading, other);
//找到当前的右侧位置,并存储在Other中
if (!Wall(other))
//如果右侧不是墙壁就向右转
{
TurnRight(heading);
MoveForward(pos, heading);
//根据当前方向向前移动一个位置
}
else
{
WheresAhead(pos, heading, other);
// 调用WheresAhead()函数,找到当前位置前方的位置
//并将结果存储在other变量中
if (!Wall(other))
//如果不是就沿着当前位置往前走
MoveForward(pos, heading);
else
//不然就向左转动
TurnLeft(heading);
}
}
posi[i] = pos;
//记录并递增
i++;
if (i >= mazeHeight * mazeWidth)
{
cout << "array too small\n";
abort();
}
int counter = 0;
int counter_dir = 0;
for (int j = 0; j < i; j++)
{
if (posi[j] < 0)
continue;
if (post[posi[j]] >= 2)
{
int empt = posi[j];
int cut_num = post[empt] - 1;
int now_num = 0;
for (int h = j + 1; h < i; h++, counter_dir++, counter++)
{
if (posi[h] == empt)
{
now_num++;
if (now_num == cut_num)
{
j = h;
counter++;
counter_dir++;
break;
}
}
}
}
cout << "Current position: (" << posi[j] / mazeWidth << ',' << posi[j] % mazeWidth << ')' << endl;
counter++;
}
cout << "total steps:" << counter << endl;
cout << "directory steps:" << counter - counter_dir << endl;
cout << "Maze solved" << endl;
}

任务三:

  • 将迷宫声明为char类型的指针。
1
2
3
4
char* maze;
int mazeWidth, mazeHeight;
int* posi;
int* post;
  • 修改LoadMaze()以动态分配迷宫的空间并将路径数组设置为适当大小的动态数组。
1
2
3
4
ifs >> mazeWidth >> mazeHeight;
maze = new char[mazeWidth * mazeHeight];
posi = new int[mazeWidth * mazeHeight];
post = new int[mazeWidth * mazeHeight];
  • 在SolveMaze()结束时删除任何分配的内存,定义FreeMaze函数,并在地图问题解决后进行调用

知识点如下:

使用 delete[] 释放动态数组。

1
2
delete p;       // p must point to a dynamically allocated object or be null
delete [] pa; // pa must point to a dynamically allocated array or be null
1
2
3
4
5
6
void FreeMaze()
{
delete[] maze;
delete[] posi;
delete[] post;
}
1
FreeMaze();

解决方案一览

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Maze.h"
using namespace std;

int main()
{
char fname[64];
//定义地图大小
cout << "Maze File: ";
cin >> fname;
if (LoadMaze(fname))
SolveMaze();
system("pause");
return 0;
}


Maze.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
/****************************************************
* Functions to solve mazes. *
* *
* Datafile must still contain size as first data. *
* *
* Four functions are only stubs. *
****************************************************/

#include <iostream>
#include <fstream>
#include "Maze.h"
using namespace std;
// The following enumerated type indicates directions within the maze
//枚举类型
enum Direction { DOWN, LEFT, UP, RIGHT };
//上左下右
// This struct is used for locations within the maze
// “这个结构用于表示迷宫中的位置
struct Position
{
int H, V;
};

// The maze itself is indicated by # for the walls
// All other locations in the maze can be any other character
// Global variables defining the maze to be solved
// 迷宫本身由#表示墙壁。迷宫中的所有其他位置可以是任何其他字符。定义了用于解决迷宫的全局变量
char* maze;
int mazeWidth, mazeHeight;
int* posi;
int* post;
int i = 0;
/*

“这些函数提供对迷宫的访问,以及操纵运动方向和迷宫位置的功能。详细信息请参阅实现部分。
*/
// These functions provide access to the maze
// as well as provide manipulation of direction
// of motion and maze location
// See implementation for details

void FindEntrance(int&);
//找到迷宫的入口,并将其存储在传入的引用参数中
bool AtExit(int);
//检验是不是迷宫的出口,是就返回True
//不然就返回false
void ReportPosition(int);
//报告当前迷宫的位置
//代码中注释了
//不被调用
void WheresRight(int, Direction, int&);
//给定当前位置和朝向,找到当前位置右侧的位置
//并将结果存储在传入的引用参数中
bool Wall(int);
//判断是不是迷宫的墙壁
//如果是墙壁
//返回True
//不然就返回false
void TurnRight(Direction&);
//右转90度
void MoveForward(int&, Direction);
//根据当前的方向移动迷宫中的位置
void WheresAhead(int, Direction, int&);
//给定当前位置和朝向,找到当前位置前方的位置
//并将结果存储在传入的引用参数中
void TurnLeft(Direction&);
//向左转动90度

// This function loads the maze from the specified file
// returning the maze and its dimensions
// The height of the maze is not actually used anywhere but here

bool LoadMaze(const char fname[])
{
ifstream ifs(fname);
// 创建了一个输入文件流ifs,并打开了指定文件名fname的文件。
// 从文件中读取迷宫的宽度和高度,并将它们存储在全局变量mazeWidth和mazeHeight中。
if (ifs.good())
{
ifs >> mazeWidth >> mazeHeight;
maze = new char[mazeWidth * mazeHeight];
posi = new int[mazeWidth * mazeHeight];
post = new int[mazeWidth * mazeHeight];
for (int i = 0; i < mazeHeight; i++)
for (int j = 0; j < mazeWidth; j++)
{
ifs >> maze[i * mazeWidth + j];
post[i * mazeWidth + j] = 0;
}
//一维数组存储
ifs.close();
return true;
}
else
{
cerr << "File not found." << endl;
return false;
}
}
//读入部分

// This function solves the maze using the 'hand on left wall'
// rule, printing the maze position as it proceeds
void FreeMaze()
{
delete[] maze;
delete[] posi;
delete[] post;
}

void SolveMaze()
//解决迷宫问题
{
int pos, other;
Direction heading;
//当前位置,其他位置
//当前的移动方向
FindEntrance(pos);
//找到入口位置,并且存储在pos变量中
heading = DOWN;
//移动方向改为向下
while (!AtExit(pos))
//只要没找到出口,就继续寻找
{
posi[i] = pos;
post[pos]++;
//记录当前到哪个位置
i++;
//进入下一个位置的记录
if (i >= mazeHeight * mazeWidth)
//如果数组超出容量,就退出
{
cout << "array too small\n";
abort();
}
WheresRight(pos, heading, other);
//找到当前的右侧位置,并存储在Other中
if (!Wall(other))
//如果右侧不是墙壁就向右转
{
TurnRight(heading);
MoveForward(pos, heading);
//根据当前方向向前移动一个位置
}
else
{
WheresAhead(pos, heading, other);
// 调用WheresAhead()函数,找到当前位置前方的位置
//并将结果存储在other变量中
if (!Wall(other))
//如果不是就沿着当前位置往前走
MoveForward(pos, heading);
else
//不然就向左转动
TurnLeft(heading);
}
}
posi[i] = pos;
//记录并递增
i++;
if (i >= mazeHeight * mazeWidth)
{
cout << "array too small\n";
abort();
}
int counter = 0;
int counter_dir = 0;
for (int j = 0; j < i; j++)
{
if (posi[j] < 0)
continue;
if (post[posi[j]] >= 2)
{
int empt = posi[j];
int cut_num = post[empt] - 1;
int now_num = 0;
for (int h = j + 1; h < i; h++, counter_dir++, counter++)
{
if (posi[h] == empt)
{
now_num++;
if (now_num == cut_num)
{
j = h;
counter++;
counter_dir++;
break;
}
}
}
}
cout << "Current position: (" << posi[j] / mazeWidth << ',' << posi[j] % mazeWidth << ')' << endl;
counter++;
}
cout << "total steps:" << counter << endl;
cout << "directory steps:" << counter - counter_dir << endl;
cout << "Maze solved" << endl;
FreeMaze();
}

// This function scans the maze array for the first non-wall item
// It assumes that the entrance is in the top row of the maze array

void FindEntrance(int& pos)
{
pos = 0;
while (Wall(pos))
{
pos++;
}
}
/*
这个函数用于寻找迷宫的入口位置。
它会从迷宫数组的第一个位置开始向后搜索,直到找到第一个非墙壁的位置为止。
找到的位置将作为迷宫的入口位置。
*/

// This function returns true if the maze position is the exit
// identified by being in the last row of the array

bool AtExit(int pos)
{
return (pos >= (mazeHeight - 1) * mazeWidth);
}
/*
这个函数用于检查当前位置是否是迷宫的出口位置。
如果当前位置位于迷宫数组的最后一行或之后,则被认为是出口位置。
如果是出口位置,则返回true,否则返回false。
*/

// This function displays the position in the maze
// At this time it specifies row and column of the array

/*void ReportPosition(int pos)
{
cout << "Current position: (" << pos/mazeWidth << ',' << pos%mazeWidth << ')' << endl;
}*/

// This function takes a maze position and a heading and determines
// the position to the right of this position

// 判断右侧位置的一个函数
// 根据当前位置和朝向,确定当前位置的右侧位置
void WheresRight(int pos, Direction heading, int& right)
{
right = pos;
switch (heading)
{
case DOWN:
{
right--;
break;
}
case LEFT:
{
right -= mazeWidth;
break;
}
case UP:
{
right++;
break;
}
case RIGHT:
{
right += mazeWidth;
}
}

}

// This function returns true if maze position is wall

//看看是不是墙壁
bool Wall(int pos)
{
return (maze[pos] == '#');
}

// This function changes heading by turning right
// Take current heading and adjust so that direction is to the right

//向右转
//经过推理为该式子
void TurnRight(Direction& heading)
{
// 当前方向向右转90度
// DOWN -> LEFT, LEFT -> UP, UP -> RIGHT, RIGHT -> DOWN
heading = static_cast<Direction>((heading + 1) % 4);
//to be finished.
}

// This function changes position in the maze by determining
// the next position in the current direction

// 判断前方位置的一个函数
// 根据当前位置和朝向,确定当前位置的前方位置
void MoveForward(int& pos, Direction heading)
{
// to be finished.
// 根据当前方向更新位置
switch (heading)
{
case DOWN:
pos += mazeWidth; // 向下移动一格
break;
case LEFT:
pos--; // 向左移动一格
break;
case UP:
pos -= mazeWidth; // 向上移动一格
break;
case RIGHT:
pos++; // 向右移动一格
break;
}
}

// This function determines the position in the direction
// currently heading

void WheresAhead(int pos, Direction heading, int& ahead)
{
//to be finished.
ahead = pos; // 初始化为当前位置

// 根据当前朝向确定前方位置
switch (heading)
{
case DOWN:
ahead += mazeWidth; //向下移动一格
break;
case LEFT:
ahead--; // 向左移动一格
break;
case UP:
ahead -= mazeWidth; // 向上移动一格
break;
case RIGHT:
ahead++; // 向右移动一格
break;
}
}

// This function changes heading by turning left

void TurnLeft(Direction& heading)
{
// 当前方向向左转90度
// DOWN -> RIGHT, LEFT -> DOWN, UP -> LEFT, RIGHT -> UP
heading = static_cast<Direction>((heading + 3) % 4);
//to be finished.
}

Maze.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once

/*****************************
* Header file for the *
* maze solver *
*****************************/
#ifndef MAZE_H
#define MAZE_H

bool LoadMaze(const char[]);
//载入
void SolveMaze();
//解决问题

#endif


实验结果

任务一通过输入maze1.txt绝对路径运行结果如下:

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
Maze File: C:/code/expert/maze1.txt
Current position: (0,13)
Current position: (1,13)
Current position: (2,13)
Current position: (3,13)
Current position: (3,12)
Current position: (3,11)
Current position: (3,10)
Current position: (3,9)
Current position: (3,8)
Current position: (3,7)
Current position: (3,7)
Current position: (4,7)
Current position: (5,7)
Current position: (5,6)
Current position: (5,5)
Current position: (5,4)
Current position: (5,3)
Current position: (5,3)
Current position: (6,3)
Current position: (7,3)
Current position: (7,3)
Current position: (7,4)
Current position: (7,5)
Current position: (7,6)
Current position: (7,7)
Current position: (7,8)
Current position: (7,9)
Current position: (8,9)
Current position: (9,9)
Current position: (9,9)
Current position: (9,10)
Current position: (9,11)
Current position: (9,12)
Current position: (9,13)
Current position: (10,13)
Current position: (11,13)
Current position: (11,12)
Current position: (11,11)
Current position: (11,11)
Current position: (12,11)
Current position: (13,11)
Current position: (13,10)
Current position: (13,9)
Current position: (12,9)
Current position: (11,9)
Current position: (11,9)
Current position: (11,9)
Current position: (12,9)
Current position: (13,9)
Current position: (14,9)
Current position: (15,9)
Current position: (15,8)
Current position: (15,7)
Current position: (14,7)
Current position: (13,7)
Current position: (12,7)
Current position: (11,7)
Current position: (10,7)
Current position: (9,7)
Current position: (9,7)
Current position: (9,6)
Current position: (9,5)
Current position: (9,5)
Current position: (10,5)
Current position: (11,5)
Current position: (11,4)
Current position: (11,3)
Current position: (10,3)
Current position: (9,3)
Current position: (9,3)
Current position: (9,2)
Current position: (9,1)
Current position: (8,1)
Current position: (7,1)
Current position: (6,1)
Current position: (5,1)
Current position: (4,1)
Current position: (3,1)
Current position: (3,2)
Current position: (3,3)
Current position: (3,4)
Current position: (3,5)
Current position: (3,5)
Current position: (2,5)
Current position: (1,5)
Current position: (1,6)
Current position: (1,7)
Current position: (1,8)
Current position: (1,9)
Current position: (1,10)
Current position: (1,11)
Current position: (1,11)
Current position: (1,11)
Current position: (1,10)
Current position: (1,9)
Current position: (1,8)
Current position: (1,7)
Current position: (1,6)
Current position: (1,5)
Current position: (1,4)
Current position: (1,3)
Current position: (1,2)
Current position: (1,1)
Current position: (1,1)
Current position: (1,1)
Current position: (1,2)
Current position: (1,3)
Current position: (1,4)
Current position: (1,5)
Current position: (2,5)
Current position: (3,5)
Current position: (3,4)
Current position: (3,3)
Current position: (3,2)
Current position: (3,1)
Current position: (3,1)
Current position: (4,1)
Current position: (5,1)
Current position: (6,1)
Current position: (7,1)
Current position: (8,1)
Current position: (9,1)
Current position: (10,1)
Current position: (11,1)
Current position: (11,1)
Current position: (11,1)
Current position: (10,1)
Current position: (9,1)
Current position: (9,2)
Current position: (9,3)
Current position: (10,3)
Current position: (11,3)
Current position: (11,3)
Current position: (11,4)
Current position: (11,5)
Current position: (11,5)
Current position: (10,5)
Current position: (9,5)
Current position: (9,6)
Current position: (9,7)
Current position: (10,7)
Current position: (11,7)
Current position: (12,7)
Current position: (13,7)
Current position: (14,7)
Current position: (15,7)
Current position: (15,6)
Current position: (15,5)
Current position: (14,5)
Current position: (13,5)
Current position: (13,5)
Current position: (13,4)
Current position: (13,3)
Current position: (13,2)
Current position: (13,1)
Current position: (13,1)
Current position: (14,1)
Current position: (15,1)
Current position: (15,1)
Current position: (15,2)
Current position: (15,3)
Current position: (15,3)
Current position: (15,3)
Current position: (15,2)
Current position: (15,1)
Current position: (14,1)
Current position: (13,1)
Current position: (13,2)
Current position: (13,3)
Current position: (13,4)
Current position: (13,5)
Current position: (14,5)
Current position: (15,5)
Current position: (15,5)
Current position: (15,6)
Current position: (15,7)
Current position: (15,8)
Current position: (15,9)
Current position: (15,9)
Current position: (14,9)
Current position: (13,9)
Current position: (13,10)
Current position: (13,11)
Current position: (13,11)
Current position: (12,11)
Current position: (11,11)
Current position: (11,12)
Current position: (11,13)
Current position: (12,13)
Current position: (13,13)
Current position: (14,13)
Current position: (15,13)
Current position: (15,12)
Current position: (15,11)
Current position: (15,11)
Current position: (15,11)
Current position: (15,12)
Current position: (15,13)
Current position: (15,13)
Current position: (14,13)
Current position: (13,13)
Current position: (13,14)
Current position: (13,15)
Current position: (14,15)
Current position: (15,15)
Current position: (16,15)
total steps:206
Maze solved

任务三通过测试maze2.txt得到结果

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
Current position: (0,20)
Current position: (1,20)
Current position: (1,19)
Current position: (2,19)
Current position: (3,19)
Current position: (3,20)
Current position: (3,21)
Current position: (4,21)
Current position: (5,21)
Current position: (5,20)
Current position: (5,19)
Current position: (6,19)
Current position: (7,19)
Current position: (7,18)
Current position: (7,17)
Current position: (7,16)
Current position: (7,15)
Current position: (8,15)
Current position: (9,15)
Current position: (10,15)
Current position: (11,15)
Current position: (11,14)
Current position: (11,13)
Current position: (12,13)
Current position: (13,13)
Current position: (13,12)
Current position: (13,11)
Current position: (12,11)
Current position: (11,11)
Current position: (11,10)
Current position: (11,9)
Current position: (12,9)
Current position: (13,9)
Current position: (13,8)
Current position: (13,7)
Current position: (13,6)
Current position: (13,5)
Current position: (13,4)
Current position: (13,3)
Current position: (13,2)
Current position: (13,1)
Current position: (14,1)
total steps:260
directory steps:42
Maze solved

任务三通过测试maze3.txt得到结果

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
Current position: (0,1)
Current position: (1,1)
Current position: (1,2)
Current position: (1,3)
Current position: (1,4)
Current position: (1,5)
Current position: (1,6)
Current position: (1,7)
Current position: (2,7)
Current position: (3,7)
Current position: (3,6)
Current position: (3,5)
Current position: (4,5)
Current position: (5,5)
Current position: (5,4)
Current position: (5,3)
Current position: (6,3)
Current position: (7,3)
Current position: (8,3)
Current position: (9,3)
Current position: (9,2)
Current position: (9,1)
Current position: (10,1)
Current position: (11,1)
Current position: (11,2)
Current position: (11,3)
Current position: (11,4)
Current position: (11,5)
Current position: (12,5)
Current position: (13,5)
Current position: (13,6)
Current position: (13,7)
Current position: (12,7)
Current position: (11,7)
Current position: (11,8)
Current position: (11,9)
Current position: (12,9)
Current position: (13,9)
Current position: (13,10)
Current position: (13,11)
Current position: (12,11)
Current position: (11,11)
Current position: (10,11)
Current position: (9,11)
Current position: (9,10)
Current position: (9,9)
Current position: (9,8)
Current position: (9,7)
Current position: (9,6)
Current position: (9,5)
Current position: (8,5)
Current position: (7,5)
Current position: (7,6)
Current position: (7,7)
Current position: (7,8)
Current position: (7,9)
Current position: (6,9)
Current position: (5,9)
Current position: (4,9)
Current position: (3,9)
Current position: (2,9)
Current position: (1,9)
Current position: (1,10)
Current position: (1,11)
Current position: (1,12)
Current position: (1,13)
Current position: (1,14)
Current position: (1,15)
Current position: (1,16)
Current position: (1,17)
Current position: (2,17)
Current position: (3,17)
Current position: (3,16)
Current position: (3,15)
Current position: (4,15)
Current position: (5,15)
Current position: (5,16)
Current position: (5,17)
Current position: (6,17)
Current position: (7,17)
Current position: (8,17)
Current position: (9,17)
Current position: (10,17)
Current position: (11,17)
Current position: (12,17)
Current position: (13,17)
Current position: (14,17)
Current position: (15,17)
Current position: (15,16)
Current position: (15,15)
Current position: (16,15)
Current position: (17,15)
Current position: (17,16)
Current position: (17,17)
Current position: (18,17)
Current position: (19,17)
Current position: (20,17)
Current position: (21,17)
Current position: (22,17)
Current position: (23,17)
Current position: (24,17)
Current position: (25,17)
Current position: (25,16)
Current position: (25,15)
Current position: (26,15)
Current position: (27,15)
Current position: (27,16)
Current position: (27,17)
Current position: (28,17)
Current position: (29,17)
Current position: (29,16)
Current position: (29,15)
Current position: (29,14)
Current position: (29,13)
Current position: (29,12)
Current position: (29,11)
Current position: (28,11)
Current position: (27,11)
Current position: (26,11)
Current position: (25,11)
Current position: (25,10)
Current position: (25,9)
Current position: (24,9)
Current position: (23,9)
Current position: (23,8)
Current position: (23,7)
Current position: (22,7)
Current position: (21,7)
Current position: (21,8)
Current position: (21,9)
Current position: (20,9)
Current position: (19,9)
Current position: (19,10)
Current position: (19,11)
Current position: (18,11)
Current position: (17,11)
Current position: (16,11)
Current position: (15,11)
Current position: (15,10)
Current position: (15,9)
Current position: (16,9)
Current position: (17,9)
Current position: (17,8)
Current position: (17,7)
Current position: (17,6)
Current position: (17,5)
Current position: (17,4)
Current position: (17,3)
Current position: (17,2)
Current position: (17,1)
Current position: (18,1)
Current position: (19,1)
Current position: (19,2)
Current position: (19,3)
Current position: (20,3)
Current position: (21,3)
Current position: (22,3)
Current position: (23,3)
Current position: (24,3)
Current position: (25,3)
Current position: (25,2)
Current position: (25,1)
Current position: (26,1)
Current position: (27,1)
Current position: (27,2)
Current position: (27,3)
Current position: (27,4)
Current position: (27,5)
Current position: (27,6)
Current position: (27,7)
Current position: (28,7)
Current position: (29,7)
Current position: (29,8)
Current position: (29,9)
Current position: (30,9)
total steps:392
directory steps:175
Maze solved

心得体会

通过本次实验,我对C++文件操作应用更加熟悉,并且阅读代码能力也有所上升,并且对基础算法的理解进一步深刻,同时,之前一直困扰我的动态内存分配也逐渐清晰,对delete和new关键字的使用也趋于熟练。