本文实例讲述了C语言实现简单的走迷宫游戏的方法,代码完整,便于读者理解。
学数据结构时用“栈”写的一个走迷宫程序,实际上用到双向队列,方便在运行完毕后输出经过的点。
?
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
|
#include <cstdio>
#include <deque>
#include <windows.h>
using namespace std;
class node
{
public :
int x,y;
int lastOpt;
};
deque<node> sta;
int x,y;
int endx,endy;
int mapW,mapH;
int steps;
int xopt[5]= {0,0,1,0,-1};
int yopt[5]= {0,1,0,-1,0};
int map[100][100]=
{
};
void init()
{
x = 1;
y = 1;
endx = 1;
endy = 9;
mapH = 10;
mapW = 10;
for ( int i=0; i<=mapH; i++)
for ( int j=0; j<=mapW; j++)
{
if (i==0 ||j==0 ||i==mapH||j==mapW)
map[i][j]=-1;
}
steps=0;
map[1][2]=-1;
map[2][2]=-1;
map[3][2]=-1;
map[4][2]=-1;
map[6][2]=-1;
map[7][2]=-1;
map[8][2]=-1;
map[9][2]=-1;
map[9][3]=-1;
map[8][3]=-1;
map[1][4]=-1;
map[3][4]=-1;
map[4][4]=-1;
map[5][4]=-1;
map[6][4]=-1;
map[7][4]=-1;
map[1][6]=-1;
map[2][6]=-1;
map[3][6]=-1;
map[4][6]=-1;
map[5][6]=-1;
map[6][6]=-1;
map[7][6]=-1;
map[8][6]=-1;
map[8][7]=-1;
map[8][8]=-1;
map[7][8]=-1;
map[6][8]=-1;
map[5][8]=-1;
map[4][8]=-1;
map[3][8]=-1;
map[2][8]=-1;
map[1][8]=-1;
map[endx][endy]=5;
}
void dis()
{
system ( "cls" );
int ori = map[x][y];
map[x][y]=1;
for ( int i=0; i<=mapH; ++i)
{
for ( int j=0; j<=mapW; ++j)
{
if (map[i][j]==0)
printf ( " " );
else if (map[i][j]==-1)
printf ( " #" );
else if (map[i][j]==1)
printf ( " @" );
else if (map[i][j]==2)
printf ( " ." );
else if (map[i][j]==5)
printf ( " !" );
}
cout<<i<<endl;
}
for ( int j=0; j<=mapW; ++j)
cout<<j<< " " ;
printf ( "\\n\\n > steps:%d Exit:(%d,%d)\\n" ,steps,endx,endy);
map[x][y] = ori;
}
int can( int n)
{
if (map[x+xopt[n]][y+yopt[n]] == 0 || map[x+xopt[n]][y+yopt[n]] == 5)
return 1;
return 0;
}
void visit( int n)
{
map[x][y]=2;
x+=xopt[n];
y+=yopt[n];
node tem;
tem.x = x;
tem.y = y;
tem.lastOpt = n;
sta.push_back(tem);
steps++;
}
int main()
{
init();
node tem;
while ( x != endx || y!=endy)
{
int cans = 0;
for ( int i=1; i<=4; i++)
{
if (can(i))
{
cans = 1;
visit(i);
break ;
}
}
if (!cans)
{
if (!sta.empty())
{
tem = sta.back();
map[tem.x][tem.y]=0;
sta.pop_back();
}
else
{
map[x][y]=2;
x+=xopt[tem.lastOpt];
x+=yopt[tem.lastOpt];
dis();
break ;
}
}
dis();
Sleep(500);
}
if (x==endx && y == endy)
cout<< "\\n > i am finished....\\n" ;
else
cout<< "\\n > i am finished...but i can't find the right way\\n" ;
return 0;
}
|
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 71
-
ASP.NET Core 3框架揭秘之 异步线程无法使用IServiceProvider问题
2025-05-29 49 -
2025-05-29 16
-
2025-05-25 98
-
详解Java中的checked异常和unchecked异常区别
2025-05-29 62
热门评论