/* 时间复杂度为O(n^2)*/
{
if(是出口)
out:
else{
1.求位置坐标a,b;
2.if(有路 &&没走过)
{
t[a][b] = 1;
a,b入栈;
Maze(a,b);
t[a][b] = 0;
a,b出栈 ;
}
}
*/
#include<stdio.h>
#define R 7
#define C 8
void Maze(int x,int y);
void Print();
int stack[100][2]; //声明一个存放栈的数组
int top = 0; //栈顶指针
int count = 0; //统计路线个数
int M[R+2][C+2]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,1,0,0,0,0,1},
{1,1,0,0,1,0,1,1,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,1,0,0,0,1,0,0,1,1},
{1,1,1,0,0,0,0,0,1,1},
{1,0,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
int t[R+2][C+2]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,1,0,0,0,0,1},
{1,1,0,0,1,0,1,1,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,1,0,0,0,1,0,0,1,1},
{1,1,1,0,0,0,0,0,1,1},
{1,0,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}, };
int Move[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; //方向数组,表示四个方向(右下左上)
int main(void)
{
stack[0][0]=stack[0][1]=1;
Maze(1,1);
}
void Maze(int x,int y)
{
int a,b;
t[x][y]=2; //路径赋予2,表示走过
if(x==R &&y==C) //走到出口,则调用输出函数
Print();
else
{
for(int i=0 ; i<4 ; i++ )
{
a=Move[i][0]+x; //新的位置坐标
b=Move[i][1]+y;
if(!M[a][b] &&!t[a][b]) //有路并且没走过
{
stack[++top][0] = a; //将位置存入栈中
stack[top][1] = b;
Maze(a,b);
t[a][b] = 0; //如果四个方位都没路,讲该点坐标清零,回溯
top--;
}
}
}
}
void Print()
{ int i,j;
printf("第%d种路线\n",++count);
printf("走过点的坐标:\n");
for( i=0 ; i<top+1 ; i++)
printf("%d,%d\n",stack[i][0]+1,stack[i][1]+1); //从栈中输出路径坐标
for( i = 0; i<R+2; i++) //输出迷宫
{
for( j = 0; j<C+2; j++)
{
printf("%2d",t[i][j]);
}
printf("\n");
}
printf("********************************************\n");
}
/*伪代码:
void Maze(int x,int y){
if(是出口)
out:
else{
1.求位置坐标a,b;
2.if(有路 &&没走过)
{
t[a][b] = 1;
a,b入栈;
Maze(a,b);
t[a][b] = 0;
a,b出栈 ;
}
}
*/
#include<stdio.h>
#define R 7
#define C 8
void Maze(int x,int y);
void Print();
int stack[100][2]; //声明一个存放栈的数组
int top = 0; //栈顶指针
int count = 0; //统计路线个数
int M[R+2][C+2]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,1,0,0,0,0,1},
{1,1,0,0,1,0,1,1,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,1,0,0,0,1,0,0,1,1},
{1,1,1,0,0,0,0,0,1,1},
{1,0,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
int t[R+2][C+2]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,1,0,0,0,0,1},
{1,1,0,0,1,0,1,1,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,1,0,0,0,1,0,0,1,1},
{1,1,1,0,0,0,0,0,1,1},
{1,0,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}, };
int Move[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; //方向数组,表示四个方向(右下左上)
int main(void)
{
stack[0][0]=stack[0][1]=1;
Maze(1,1);
}
void Maze(int x,int y)
{
int a,b;
t[x][y]=2; //路径赋予2,表示走过
if(x==R &&y==C) //走到出口,则调用输出函数
Print();
else
{
for(int i=0 ; i<4 ; i++ )
{
a=Move[i][0]+x; //新的位置坐标
b=Move[i][1]+y;
if(!M[a][b] &&!t[a][b]) //有路并且没走过
{
stack[++top][0] = a; //将位置存入栈中
stack[top][1] = b;
Maze(a,b);
t[a][b] = 0; //如果四个方位都没路,讲该点坐标清零,回溯
top--;
}
}
}
}
void Print()
{ int i,j;
printf("第%d种路线\n",++count);
printf("走过点的坐标:\n");
for( i=0 ; i<top+1 ; i++)
printf("%d,%d\n",stack[i][0]+1,stack[i][1]+1); //从栈中输出路径坐标
for( i = 0; i<R+2; i++) //输出迷宫
{
for( j = 0; j<C+2; j++)
{
printf("%2d",t[i][j]);
}
printf("\n");
}
printf("********************************************\n");
}