- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C++利用递归实现走迷宫由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例为大家分享了C++利用递归实现走迷宫的具体代码,供大家参考,具体内容如下 。
要求:
1、将地图的数组保存在文件中,从文件中读取行列数 。
2.、动态开辟空间保存地图 。
3.、运行结束后再地图上标出具体的走法 。
说明:
1、文件中第一行分别放置的是地图的行数和列数 。
2、其中1表示墙,即路不通,0表示路,即通路 。
3、程序运行结束后用2标记走过的路径 。
4、当走到“死胡同”时用3标记此路为死路 。
5、每到一个点,按照 左 上 右 下 的顺序去试探 。
6、没有处理入口就是"死胡同"的极端情况 。
maze.h 。
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
|
#ifndef _MAZE_H_
#define _MAZE_H_
#include <fstream> // ifstream
#include <iostream>
#include <cassert>
#include <string>
using
namespace
std;
// 坐标类
class
Seat
{
public
:
Seat(
int
_x,
int
_y)
:x(_x)
,y(_y)
{ }
int
x;
int
y;
};
// 迷宫类
class
Maze
{
private
:
int
** _map;
// 指向地图的指针
int
_row;
// 存放地图的行数
int
_col;
// 存放地图的列数
public
:
// 构造函数 读取文件里的地图 和行数
Maze(
const
string& filePath);
private
:
// 判断是否是路
bool
IsPass(Seat& entry);
public
:
// 开始走
bool
PassMaze( Seat& entry);
// 打印地图
void
PrintMap();
// 析构 释放空间
~Maze();
};
#endif
|
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
|
// 递归实现迷宫
#include "maze.h"
// 判断是否是路
bool
Maze::IsPass( Seat& Entry)
{
if
(_map[Entry.x][Entry.y] == 0)
{
return
true
;
}
return
false
;
}
// 构造函数 读取文件里的地图 和行数
Maze::Maze(
const
string& filePath )
{
ifstream read(filePath);
string str_row_col, str_temp;
// 读取第一行
getline(read, str_row_col);
// 获取行
str_temp = str_row_col.substr(0, str_row_col.find_first_of(
','
));
_row =
atoi
(str_temp.c_str());
// 获取列
str_temp = str_row_col.substr( str_row_col.find_first_of(
','
)+1);
_col =
atoi
(str_temp.c_str());
// 为地图分配空间
_map =
new
int
*[_row];
for
(
int
index = 0; index < _col; ++index)
{
_map[index] =
new
int
[_col];
}
int
index_col = 0;
int
index_row = 0;
// 填充地图
while
(!read.eof())
{
// 获取一行
getline(read, str_temp);
char
* line = (
char
*)str_temp.c_str();
while
((*line) !=
'\0'
)
{
if
(*line ==
'0'
|| *line ==
'1'
)
{
_map[index_row][index_col++] = *line -
'0'
;
}
++line;
}
++index_row;
index_col = 0;
}
// 关闭文件
read.close();
}
// 开始走
bool
Maze::PassMaze( Seat& Entry)
{
// 判断是否走到出口
if
(Entry.x < 0 || Entry.y < 0 || Entry.y >=_row)
{
return
true
;
}
if
(IsPass(Entry))
{
// 将走过的路置为2
_map[Entry.x][Entry.y] = 2;
// 向左走
Seat left(Entry.x, Entry.y-1);
if
(PassMaze(left))
// 递归调用
{
return
true
;
}
// 向上走
Seat up(Entry.x-1, Entry.y);
if
(PassMaze(up))
// 递归调用
{
return
true
;
}
// 向右走
Seat right(Entry.x, Entry.y+1);
if
(PassMaze(right))
// 递归调用
{
return
true
;
}
// 向下走
Seat down(Entry.x+1, Entry.y);
if
(PassMaze(down))
{
return
true
;
}
// 走到此处说明是死路 置为3
_map[Entry.x][Entry.y] = 3;
}
return
false
;
}
// 打印地图
void
Maze::PrintMap()
{
for
(
int
row = 0; row<_row; ++row)
{
for
(
int
col = 0; col<_col; ++col)
{
cout << _map[row][col] <<
" "
;
}
cout <<endl;
}
cout <<endl;
}
// 释放空间
Maze::~Maze()
{
for
(
int
idx = 0; idx < _row; ++idx )
{
delete
[] _map[idx];
}
delete
[] _map;
_map = NULL;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/a1414345/article/details/53861500 。
最后此篇关于C++利用递归实现走迷宫的文章就讲到这里了,如果你想了解更多关于C++利用递归实现走迷宫的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想递归地遍历一个目录,但我希望 python 在遇到包含超过 100 个文件的目录时从任何单个 listdir 中断。基本上,我正在搜索 (.TXT) 文件,但我想避免使用包含大型 DPX 图像序列
我正在尝试遍历列表(例如 sql 行)并为每一行触发例程。问题是传递给函数的值不会在运行时进行评估,因此根据函数执行所需的时间,它可能会使用下一行中的任何值而不是当前行。 我知道我可以在普通函数中提取
我需要以毫秒为单位的时间来处理大量事务,因此我想要正确且快速的东西。下面的工作会做得最好吗? : iMilli := int((time.Nanoseconds() % 1e6) / 1e3
我有以下目录/文件设置(已简化): Ce +---top.txt +---X0.0 | | | +---Y0.0 | | | | |
我遇到了类似的问题: Connecting to Redis To Go with PHP 基本上,我在 redis 中有这个 uri: redis://myusername:foopassword@
我阅读了下面的主题 Go: multiple value in single-value context 但我不明白这个解释在我的案例中。可能是因为我想使用 interface 在下面的情况下,我得到
我有一个模板,我想使用 text/template 评估各个字段包裹。我很难弄清楚评估应该如何工作,因为下面的代码似乎失败了。模板包是否足够强大以处理此类评估? type something stru
我编写了简单的服务器程序来从客户端接收数据。我有点不明白有时我从函数中得到错误 read tcp4 IP:PORT i/o timeoutint, err := conn.Read([]byte) 未
我只需要解码和更新 json 对象的特定值。问题是我不知道对象的完整结构。 encoding/json 包“忽略”/截断结构中未提供的字段,因此在编码时这些字段将丢失。 我想知道是否可以只解码我知道的
我正在尝试使用带有 C++ 目标的 ANTLR4 来实现 TSql 解析器。我抓取了语法文件 here .该jar用于制作相应的源文件(因冲突将TSqlParser.cpp中的NULL全部改为null
我在 win7 中使用 python 3.3.3 - 我只想列出网络目录中的所有文件。 import os for root, dirs, files in os.walk("X:\\network\
当我运行 go 脚本 ( go run example.go ) 时出现此错误 /home/travis/.gvm/gos/go1.1.2/src/pkg/github.com/user/exampl
我正在尝试通过 gmail API 发送电子邮件使用 Go但我发现文档非常有缺陷/令人困惑。这一次我看不到收据字段和电子邮件正文。 我不需要上传任何东西,所以我找到了 Simple upload ,
本人是一名专业的windows/.Net开发者,一直在慢慢学习rails/ruby/python/etc。在我有空的时候。在过去 8 年左右的时间里,我也一直在使用各种 Linux 发行版。然而,有一
我想知道是否可以使用 std http 来响应 http 请求打包并仍然保持 go 例程事件(例如运行任务密集型任务)。用例是我需要接收一个 http 请求,然后在几分钟后回调该服务 最佳答案 只需从
我想知道关于指针的最佳实践是什么。我应该在结构上还是在其字段上定义它们。我虽然定义一个指向结构本身的指针是有意义的,但这里有一个我觉得很有趣的例子。如果所有字段都是指针,为什么我不应该使用指向整个结构
我是一名优秀的程序员,十分优秀!