当前位置:网站首页>C语言实现迷宫问题
C语言实现迷宫问题
2022-06-11 21:36:00 【爱学代码的学生】
1. 首先创建地图
int i=0,j=0;
int a[8][8];
for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(i==0||i==7||j==0||j==7){
a[i][j]=1;
}else{
a[i][j]=0;
}
}
}
srand((unsigned)time(NULL));
int m=rand()%6+1;
int n=rand()%6+1;
a[m][n]=1;
printf("=====地图情况=====\n");
for(i=0;i<8;i++){
for(j=0;j<8;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
这里我们就得到了地图的情况,我们这里使用了srand函数来随机生成一个障碍物。
接下来我们就应该实现走迷宫
2. 走迷宫
bool FindWay(int a[8][8],int i,int j){
if(a[6][6]==2){
printf("到达世界最高城理塘!!\n");
return true;
}else{
if(a[i][j]==0){
a[i][j]=2;//表示已经走过该位置
if(FindWay(a,i,j+1)){ //上下左右先后顺序随意
return true;
}else if(FindWay(a,i+1,j)){
return true;
}else if(FindWay(a,i-1,j)){
return true;
}else if(FindWay(a,i,j-1)){
return true;
}
else{
a[i][j]=3;//此路不可通
return false;
}
}else{//已经走过或者遇见障碍物或者此路不可通
return false;
}
}
}这样我们就可以实现简单迷宫问题
如果感兴趣的话可以尝试改变顺序来改变路径。
边栏推荐
- Builder pattern
- EndnoteX9简介及基本教程使用说明
- Expérience 10 génération de courbes bezier - amélioration expérimentale - génération de courbes B - spline par point de contrôle
- Codeforces Round #744 (Div. 3) 解题报告
- Leetcode-98- validate binary search tree
- Codeforces Round #739 (Div. 3)解题报告
- LabVIEW controls Arduino to realize ultrasonic ranging (advanced chapter-5)
- Deploy SAP ui5 applications to the sap BTP kyma operating environment step by step
- Building a custom CNN model: identifying covid-19
- LeetCode-104-二叉树的最大深度
猜你喜欢

LeetCode-110-平衡二叉树

189. 轮转数组

Flutter series: detailed explanation of container layout commonly used in flutter

Leetcode-104- maximum depth of binary tree

Jenkins+allure integrated report construction

多态的所有特征
![BZOJ3189 : [Coci2011] Slika](/img/46/c3aa54b7b3e7dfba75a7413dfd5b68.png)
BZOJ3189 : [Coci2011] Slika

LabVIEW控制Arduino实现红外测距(进阶篇—6)

Software test plan

The same efficiency tool for leading enterprises to promote smart finance. Let's have a quick look?
随机推荐
Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称
Builder pattern
Building a custom CNN model: identifying covid-19
Leetcode-76- minimum covering substring
类和对象(2)
RPA+低代码为何是加速财务数字化转型之利器?
Redis basic data type (set)
Codeforces Round #744 (Div. 3) 解题报告
Only 38 years old! Zhou Chuan, a researcher of the Chinese Academy of Sciences, died unfortunately. Rao Yi sent a document to mourn: he guided me when he was still my student
LabVIEW控制Arduino实现红外测距(进阶篇—6)
LeetCode-32-最长有效括号
8、 BOM - chapter after class exercises and answers
效率起飞啊!还能这样开发的?
Redis basic data type (Zset) ordered collection
2021 Niuke multi school 5 double strings
Redis transaction
Rexroth overflow valve zdb6vp2-42/315v
Bipartite King
EndnoteX9简介及基本教程使用说明