- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C++迷宫问题的求解算法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例为大家分享了C++实现迷宫的具体代码,供大家参考,具体内容如下 。
1、 实验目的:
(1) 熟练掌握链栈的基本操作及应用。 (2) 利用链表作为栈的存储结构,设计实现一个求解迷宫的非递归程序.
2、实验内容:
【问题描述】 。
以一个m×n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对信任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论.
【基本要求】 。
首先实现一个链表作存储结构的栈类型,然后编写一个求解迷宫的非递归程序。求得的通路以三元组(i,j,d)的形式输出,其中:(i,j)指示迷宫中的一个坐标,d表示走到下一坐标的方向。如:对于下列数据的迷宫,输出的一条通路为:(1,1,1),(1,2,2),(2,2,2),(3,2,3),(3,1,2),…….
【测试数据】/strong> 。
迷宫的测试数据如下:左上角(1,1)为入口,右下角(8,9)为出口。 1 2 3 4 5 6 7 8 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 。
【实现提示】 。
计算机解迷宫通常用的是“穷举求解”方法,即从入口出发,顺着某一个方向进行探索,若能走通,则继续往前进;否则沿着原路退回,换一个方向继续探索,直至出口位置,求得一条通路。假如所有可能的通路都探索到则未能到达出口,则所设定的迷宫没有通睡。 可以二维数组存储迷宫数据,通常设定入口点的下标为(1,1),出口点的下标为(n,n)。为处理方便起见,可以迷宫的四周加一圈障碍。对于迷宫任一位置,均可约定有东、南、西、北四个方向可通.
【选作内容】 。
(1) 编写递归形式的算法,求得迷宫中所有可能的通路; (2) 以方阵形式输出迷宫及其通路.
网友提供了一段解决算法:
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
|
#include<stdio.h>
#include<stdlib.h>
#define m 4//行
#define n 4//列
struct
xy
{
int
x;
int
y;
};
typedef
struct
stack
{
struct
xy coordinate;
struct
stack* next;
}stack;
void
init(stack* p)
{
p->next = NULL;
}
void
push(stack* p,
struct
xy cdnt)
{
stack* temp = p;
while
(temp->next != NULL)
temp = temp->next;
stack* newValue = (stack*)
malloc
(
sizeof
(
struct
stack)*1);
newValue->coordinate = cdnt;
newValue->next = temp->next;
temp->next = newValue;
}
void
pop(stack* p)
{
stack* tempp = p;
stack* temp = p->next;
while
(temp->next != NULL)
temp = temp->next,tempp = tempp->next;
tempp->next = NULL;
free
(temp);
}
void
browse(stack* p)
{
stack* temp = p->next;
while
(temp != NULL)
printf
(
"(%d,%d)\n"
,temp->coordinate.y,temp->coordinate.x),temp = temp->next;
}
struct
xy getEnd(
struct
stack* p)
{
stack* temp = p;
while
(temp->next != NULL)
temp = temp->next;
return
temp->coordinate;
}
int
getSize(stack* p)
{
int
size = 0;
stack* temp = p->next;
while
(temp != NULL)
{
size++;
temp = temp->next;
}
return
size;
}
int
main()
{
int
path[m+1][n+1] = {0};
int
col = 0,row = 0;
int
i = 0,j = 0;
int
temp_col = 0,temp_row = 0,t_col = 0,t_row = 0;
int
flag = 0;
struct
xy t_pair;
//stack A,B;
stack* Ahead = (stack*)
malloc
(
sizeof
(
struct
stack)*1);
stack* Bhead = (stack*)
malloc
(
sizeof
(
struct
stack)*1);
init(Ahead); init(Bhead);
for
(;i<m;i++)
for
(j=0;j<n;j++)
{
printf
(
"input 0 or 1\n"
);
scanf
(
"%d"
,&path[i][j]);
}
i = j = 0;
if
(path[0][0] == 0 || path[m-1][n-1] == 0)
{
printf
(
"There is no way\n"
);
return
1;
}
while
(1)
{
//检验是否已经到达末尾
if
(col == n-1 && row == m-1 && path[row][col] == 1)
{
//到达尾端,意味着找到一条路
flag = 1;
printf
(
"Find a way,it's\n"
);
browse(Ahead);
printf
(
"(%d,%d)\n"
,m-1,n-1);
if
(getSize(Bhead) != 0)
{
temp_col = getEnd(Bhead).x;
temp_row = getEnd(Bhead).y;
while
(1)
if
(getEnd(Ahead).x == temp_col && getEnd(Ahead).y == temp_row)
break
;
else
pop(Ahead);
col = temp_col + 1;
row = temp_row;
pop(Bhead);
}
else
return
1;
}
else
//还没有到末尾的情况
{
if
(path[row + 1][col] == 1 && path[row][col + 1] == 1)
{
t_pair.x = col;t_pair.y = row;
push(Ahead,t_pair);
push(Bhead,t_pair);
row++;
continue
;
}
//下面不是右边也不是
if
(path[row + 1][col] == 0 && path[row][col + 1] == 0)
{
if
(getSize(Bhead))
{
//vector<struct xy>::iterator iter = B.end() - 1;
col = getEnd(Bhead).x + 1;row = getEnd(Bhead).y;
//回到上一次分叉处,搜索右侧路径
pop(Ahead);
pop(Bhead);
continue
;
}
else
return
1;
}
//下面是,右边不是
if
(path[row + 1][col] == 1 && path[row][col + 1] == 0)
{
t_pair.x = col;t_pair.y = row;
push(Ahead,t_pair);
row++;
continue
;
}
//下面不是,右边是
if
(path[row + 1][col] == 0 && path[row][col + 1] == 1)
{
t_pair.x = col;t_pair.y = row;
push(Ahead,t_pair);
col++;
continue
;
}
}
}
if
(!flag)
printf
(
"There is no way\n"
);
return
0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
最后此篇关于C++迷宫问题的求解算法的文章就讲到这里了,如果你想了解更多关于C++迷宫问题的求解算法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
滑动窗口限流 滑动窗口限流是一种常用的限流算法,通过维护一个固定大小的窗口,在单位时间内允许通过的请求次数不超过设定的阈值。具体来说,滑动窗口限流算法通常包括以下几个步骤: 初始化:设置窗口
表达式求值:一个只有+,-,*,/的表达式,没有括号 一种神奇的做法:使用数组存储数字和运算符,先把优先级别高的乘法和除法计算出来,再计算加法和减法 int GetVal(string s){
【算法】前缀和 题目 先来看一道题目:(前缀和模板题) 已知一个数组A[],现在想要求出其中一些数字的和。 输入格式: 先是整数N,M,表示一共有N个数字,有M组询问 接下来有N个数,表示A[1]..
1.前序遍历 根-左-右的顺序遍历,可以使用递归 void preOrder(Node *u){ if(u==NULL)return; printf("%d ",u->val);
先看题目 物品不能分隔,必须全部取走或者留下,因此称为01背包 (只有不取和取两种状态) 看第一个样例 我们需要把4个物品装入一个容量为10的背包 我们可以简化问题,从小到大入手分析 weightva
我最近在一次采访中遇到了这个问题: 给出以下矩阵: [[ R R R R R R], [ R B B B R R], [ B R R R B B], [ R B R R R R]] 找出是否有任
我正在尝试通过 C++ 算法从我的 outlook 帐户发送一封电子邮件,该帐户已经打开并记录,但真的不知道从哪里开始(对于 outlook-c++ 集成),谷歌也没有帮我这么多。任何提示将不胜感激。
我发现自己像这样编写了一个手工制作的 while 循环: std::list foo; // In my case, map, but list is simpler auto currentPoin
我有用于检测正方形的 opencv 代码。现在我想在检测正方形后,代码运行另一个命令。 代码如下: #include "cv.h" #include "cxcore.h" #include "high
我正在尝试模拟一个 matlab 函数“imfill”来填充二进制图像(1 和 0 的二维矩阵)。 我想在矩阵中指定一个起点,并像 imfill 的 4 连接版本那样进行洪水填充。 这是否已经存在于
我正在阅读 Robert Sedgewick 的《C++ 算法》。 Basic recurrences section it was mentioned as 这种循环出现在循环输入以消除一个项目的递
我正在思考如何在我的日历中生成代表任务的数据结构(仅供我个人使用)。我有来自 DBMS 的按日期排序的任务记录,如下所示: 买牛奶(18.1.2013) 任务日期 (2013-01-15) 任务标签(
输入一个未排序的整数数组A[1..n]只有 O(d) :(d int) 计算每个元素在单次迭代中出现在列表中的次数。 map 是balanced Binary Search Tree基于确保 O(nl
我遇到了一个问题,但我仍然不知道如何解决。我想出了如何用蛮力的方式来做到这一点,但是当有成千上万的元素时它就不起作用了。 Problem: Say you are given the followin
我有一个列表列表。 L1= [[...][...][.......].......]如果我在展平列表后获取所有元素并从中提取唯一值,那么我会得到一个列表 L2。我有另一个列表 L3,它是 L2 的某个
我们得到二维矩阵数组(假设长度为 i 和宽度为 j)和整数 k我们必须找到包含这个或更大总和的最小矩形的大小F.e k=7 4 1 1 1 1 1 4 4 Anwser是2,因为4+4=8 >= 7,
我实行 3 类倒制,每周换类。顺序为早类 (m)、晚类 (n) 和下午类 (a)。我固定的订单,即它永远不会改变,即使那个星期不工作也是如此。 我创建了一个函数来获取 ISO 周数。当我给它一个日期时
假设我们有一个输入,它是一个元素列表: {a, b, c, d, e, f} 还有不同的集合,可能包含这些元素的任意组合,也可能包含不在输入列表中的其他元素: A:{e,f} B:{d,f,a} C:
我有一个子集算法,可以找到给定集合的所有子集。原始集合的问题在于它是一个不断增长的集合,如果向其中添加元素,我需要再次重新计算它的子集。 有没有一种方法可以优化子集算法,该算法可以从最后一个计算点重新
我有一个包含 100 万个符号及其预期频率的表格。 我想通过为每个符号分配一个唯一(且前缀唯一)的可变长度位串来压缩这些符号的序列,然后将它们连接在一起以表示序列。 我想分配这些位串,以使编码序列的预
我是一名优秀的程序员,十分优秀!