#include<stdio.h>
#include<windows.h>
char c[][20]=
{
"************",
"*00000*0000*",
"*0***000*0&*",
"*000*0**0***",
"***000000***",
"************"
};
int row = sizeof(c)/sizeof(c[0]);
void bulidmaze(char c[][20],int count)
{
int n=0;
for(n;n<count;n++)
{
printf("%s\r\n",c[n]);
}
}
int test(char c[][20],int x,int y)
{
if(c[x][y]=='&')
{
printf("找到出口\n");
exit(0);
}
c[x][y]='@';
_sleep(1000);
system("cls");
bulidmaze(c,row);
//system("pause");
if('0'==c[x+1][y]||'&'==c[x+1][y])
{
test(c,x+1,y);
}
if('0'==c[x][y+1]||'&'==c[x][y+1])
{
test(c,x,y+1);
}
if('0'==c[x-1][y]||'&'==c[x-1][y])
{
test(c,x-1,y);
}
if('0'==c[x][y-1]||'&'==c[x][y-1])
{
test(c,x,y-1);
}
c[x][y]='S';
return;
}
int main()
{
//生成迷宫
bulidmaze(c,row);
//探索路线 1,1为入口
test(c,1,1);
return 0;
}
【C语言】用递归走迷宫
最新推荐文章于 2023-10-24 14:26:33 发布