|
|
|
@ -101,16 +101,83 @@
|
|
|
|
|
|
|
|
|
|
可以配合程序代码加以说明。如:
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
struct LNode {
|
|
|
|
|
E data; // 数据元素
|
|
|
|
|
LNode *next; // 指向下一个结点的指针
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
int maze[M][N],row,col;
|
|
|
|
|
typedef struct 栈操作函数
|
|
|
|
|
void Init_hand_Maze(int maze[M][N],int m,int n)
|
|
|
|
|
{int i,j;
|
|
|
|
|
for(i=1;i<=m+1;i++)
|
|
|
|
|
for(j=1;j<=n+1;j++)
|
|
|
|
|
{maze[i][j]=1;
|
|
|
|
|
}
|
|
|
|
|
cout<<" 请按行输入迷宫, 0表示通路, 1 表示障碍:"<<endl; for(i=1;i<m+1;i++)
|
|
|
|
|
for(j=1.j<n+1;j++)
|
|
|
|
|
cin>>maze[i][j];
|
|
|
|
|
for(i=1;i<m+1;i++){
|
|
|
|
|
for(j=1:j<n+1;j++)
|
|
|
|
|
{
|
|
|
|
|
if(maze[i][j]!=0&&maze[i][j]!=1)(
|
|
|
|
|
cout<<” 您输入有误,请重新输入";
|
|
|
|
|
Init_hand_Maze(maze,m,n);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
时间复杂度 O(m*n)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
对该数据结构的特点进行分析。
|
|
|
|
|
void Init_automatic_Maze(int maze[M][N],int m,int n) 求解迷宫
|
|
|
|
|
Status MazePath(Stack &S,MazeType &e,int maze[M][N], int m, int n)
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if(maze[][]==0) 打印路径
|
|
|
|
|
int PrintPath(Stack S,int maze[M][N],int row,int col)
|
|
|
|
|
{
|
|
|
|
|
if==
|
|
|
|
|
cout<<"\n===============================================\n";
|
|
|
|
|
cout<<" 此迷宫无解 \n\n";
|
|
|
|
|
return ERROR;
|
|
|
|
|
}
|
|
|
|
|
MazeType e; while!=
|
|
|
|
|
{Pop(S,e); maze[][]=+10);}
|
|
|
|
|
cout<<"\n===============================================\n";
|
|
|
|
|
cout<<" 路径为 :"<<endl;
|
|
|
|
|
int i,j:
|
|
|
|
|
for(i=1;i<row+1;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=1;j<col+1:j++)
|
|
|
|
|
{
|
|
|
|
|
switch(maze[i][j])
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
cout<<" 口";
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
cout<<" ■";
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
cout<<"※”;
|
|
|
|
|
break;
|
|
|
|
|
case 10:
|
|
|
|
|
cout<<" →";
|
|
|
|
|
break;
|
|
|
|
|
case 11:
|
|
|
|
|
cout<<" ↓";
|
|
|
|
|
break;
|
|
|
|
|
case 12:
|
|
|
|
|
cout<<" ←";
|
|
|
|
|
break;
|
|
|
|
|
case 13:
|
|
|
|
|
cout<<" ↑";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout<<endl;
|
|
|
|
|
}
|
|
|
|
|
cout<<" 完成!"<<endl;
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -120,14 +187,33 @@ struct LNode {
|
|
|
|
|
|
|
|
|
|
可以配合程序代码加以说明。如:
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
// 冒泡排序
|
|
|
|
|
void bubble_sort(T a[], int n)
|
|
|
|
|
主函数
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
......
|
|
|
|
|
switch(i)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
Init_hand_Maze(maze,m,n);
|
|
|
|
|
PrintMaze(maze,m,n);
|
|
|
|
|
InitStack(S);
|
|
|
|
|
MazePath(S,start,maze,,;
|
|
|
|
|
PrintPath(S,maze,m,n);
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
Init_automatic_Maze(maze,m,n);
|
|
|
|
|
PrintMaze(maze,m,n);
|
|
|
|
|
initStack(S):
|
|
|
|
|
MazePath(S,start,maze,,;
|
|
|
|
|
PrintPath(S,maze,m,n);
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
cycle=(-1);break;
|
|
|
|
|
default:
|
|
|
|
|
cout<<"\n";cout<<" 你的输入有误!\n”;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
对该算法的时间和空间复杂度进行分析。
|
|
|
|
|