#include<stdio.h>
#include<conio.h>
int migong[10][10]= //设置迷宫,最外围1为墙 里边0为可走路径 1为障碍
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1,1,1},
{1,0,1,1,1,1,1,0,0,1},
{1,0,1,0,0,0,0,0,0,1},
{1,0,0,0,1,0,1,1,1,1},
{1,1,1,1,0,0,1,1,1,1},
{1,0,0,0,0,1,1,1,1,1},
{1,0,1,1,0,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int num;
struct
{
int x,y,d;
}lj[100];//x,y分别为垂直和水平方向
void start()
{
int top=0,x,y,d,find;//d为设置方向,上下左右。find为设置找不找得到路
lj[top].x=1;
lj[top].y=1;
migong[1][1]=-1;
find=0;d=-1;
while(top>-1){
if(lj[top].x==8&&lj[top].y==8)
{
printf("迷宫路径如下:\n");
printf("start->");
for(x=0;x<=top;x++)
{
printf("(%d,%d)-> ",lj[x].x,lj[x].y);//把找到的路径输出
num++;
if(num%8==0)
printf("\n");
}
printf("->end!\n");
}
while(d<4&&find==0){
d++;
switch(d){
case 0:x=lj[top].x-1; y=lj[top].y; break;//方向为上
case 1:x=lj[top].x; y=lj[top].y+1;break;//方向为右
case 2:x=lj[top].x+1; y=lj[top].y; break;//方向为下
case 3:x=lj[top].x; y=lj[top].y-1;}//方向为左
if(migong[x][y]==0)
find=1;
}
if(find==1){ //判断是否找得到
lj[top].d=d;
top++;
lj[top].x=x;
lj[top].y=y;
d=-1;find=0; //重新调整方向
migong[x][y]=-1;}
else{
migong[lj[top].x][lj[top].y]=0;
top--;d=lj[top].d; //找不到的话退栈
}
}
}
void main()
{
start();
getch();
}
#include<stdio.h>
#include<conio.h>
int migong[10][10]= //设置迷宫,最外围1为墙 里边0为可走路径 1为障碍
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1,1,1},
{1,0,1,1,1,1,1,0,0,1},
{1,0,1,0,0,0,0,0,0,1},
{1,0,0,0,1,0,1,1,1,1},
{1,1,1,1,0,0,1,1,1,1},
{1,0,0,0,0,1,1,1,1,1},
{1,0,1,1,0,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int num;
struct
{
int x,y,d;
} lj[100]; //x,y分别为垂直和水平方向
void start()
{
int top=0,x,y,d,find;//d为设置方向,上下左右。find为设置找不找得到路
lj[top].x=1;//设置当前x,y的坐标初始为1
lj[top].y=1;
migong[1][1]=-1;//代表(1,1)走过了
find=0;
d=-1;
while(top>-1)
{
if(lj[top].x==8&&lj[top].y==8)//如果到达(8,8)结束,输出路径
{
printf("迷宫路径如下:\n");
printf("start->");
for(x=0; x<=top; x++)
{
printf("(%d,%d)-> ",lj[x].x,lj[x].y);//把找到的路径输出
num++;
if(num%8==0)//每8个占一行
printf("\n");
}
printf("->end!\n");
}
while(d<4&&find==0)//未到达(8,8)
{
d++;//d代表4个方向
switch(d)
{
case 0:
x=lj[top].x-1;
y=lj[top].y;
break;//方向为上
case 1:
x=lj[top].x;
y=lj[top].y+1;
break;//方向为右
case 2:
x=lj[top].x+1;
y=lj[top].y;
break;//方向为下
case 3:
x=lj[top].x;
y=lj[top].y-1;
}//方向为左
if(migong[x][y]==0)//0为可走路径 设置find=1代表能走
find=1;
}
if(find==1) //判断是否找得到
{
lj[top].d=d;//记录当前的方向
top++;
lj[top].x=x;//记录当前的坐标
lj[top].y=y;
d=-1;
find=0; //重新调整方向
migong[x][y]=-1;//代表(x,y)走过了
}
else//find=0找不到
{
migong[lj[top].x][lj[top].y]=0;
top--;
d=lj[top].d; //找不到的话退栈
}
}
}
void main()
{
start();
getch();
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m