📄 migong.cpp
字号:
#include"stdio.h"
#define N 10
int c=0;
typedef struct {int r;int c;} postype;//坐标类型
void print(int maze[][N]) //打印迷宫
{
int i,j;
for(i=0;i<N;i++)
{
printf("\n");
for(j=0;j<N;j++)
printf("%-4d", maze[i][j]);
}
}
void mazepath(int m[][N], postype curpos, postype end , int count)
{//求当前信置(即:第count步)开始到出口的路径
int i,j,k,x,y;
postype s;
i=curpos.r; j=curpos.c;
m[i][j]=count;
if(i==end.r&&j==end.c) { c++; print(m);getchar();}//如果到出口,则输出
else //试探当前位置的四个方向
{
for(k=1;k<=4;k++)
{
x=i;y=j;
switch(k){
case 1: y++;break;
case 2: x++;break;
case 3: x--;break;
case 4: y--;
}
if (m[x][y]==0) { s.r=x; s.c=y; mazepath(m,s,end,count+1);}
}
}
m[i][j]=0;//退回一步
}
void main()
{
int maze[N][N]={{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,0,0,-1,0,0,0,-1,0,-1},
{-1,0,0,-1,0,0,0,-1,0,-1},{-1,0,0,0,0,-1,-1,0,0,-1},{-1,0,-1,-1,-1,0,0,0,0,-1},
{-1,0,0,0,-1,0,0,0,0,-1},{-1,0,-1,0,0,0,-1,0,0,-1},{-1,0,-1,-1,-1,0,-1,-1,0,-1},
{-1,-1,0,0,0,0,0,0,0,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}};
int count=1;
postype start,end;
start.r=start.c=1; end.r=end.c=8;
print(maze);
//getchar();//为了观察迷宫,让输出有所停顿
mazepath(maze,start,end,count);
printf("c=%d\n",c);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -