数据结构(C语言)课设7——迷宫求解
题目描述:
以一个m*n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。
#include <iostream>
#include <malloc.h>
using namespace std;
#define MAXSIZE 100
typedef struct
{
int x;
int y;
}Coordinate;
typedef struct
{
Coordinate position;
int direction;
}CoordinateDir;
typedef struct
{
CoordinateDir *top;
CoordinateDir *base;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base = new CoordinateDir[MAXSIZE];
if(!S.base){
cout<<"存储分配错误"<<endl;
}
S.top = S.base;
S.stacksize = MAXSIZE;
}
bool isEmpty(SqStack &S)
{
if(S.base == S.top)
{
return true;
}
return false;
}
void Push(SqStack &S, CoordinateDir &e)
{
if(S.top-S.base == S.stacksize){
cout<<"栈满"<<endl;
}
*S.top++ = e;
}
void Pop(SqStack &S, CoordinateDir &e)
{
if(S.base == S.top){
cout<<"空栈"<<endl;
}
e = *--S.top;
}
Coordinate R_position;
Coordinate C_position;
bool isPass(int **a, Coordinate &Coo)
{
if(a[Coo.x][Coo.y] == 0)
{
return false;
}
return true;
}
void signRoad(int **a, Coordinate &Coo)
{
a[Coo.x][Coo.y] = 2;
}
void SetWall(int **a, Coordinate &Coo)
{
a[Coo.x][Coo.y] = 1;
}
CoordinateDir isGo(int **a, CoordinateDir &Dir)
{
switch(Dir.direction)
{
case 1: Dir.position.x++;
if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
break;
Dir.position.x--;
case 2: Dir.direction = 2;
Dir.position.y++;
if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
break;
Dir.position.y--;
case 3: Dir.direction = 3;
Dir.position.x--;
if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
break;
Dir.position.x++;
case 4: Dir.direction = 4;
Dir.position.y--;
if((a[Dir.position.x][Dir.position.y] == 2) && (a[Dir.position.x][Dir.position.y] == 1))
{
SetWall(a, Dir.position);
Dir.position.y++;
}
break;
}
return Dir;
}
void create_Maze(int **&a)
{
int n, m, load;
cout<<"请输入迷宫的长和宽:";cin>>n>>m;
cout<<"请输入迷宫(通路由0表示,障碍由1表示):"<<endl;
a= new int *[50];
for(int i = 1; i <= n; i++)
{a[i] = new int[50];
for(int j = 1; j <= m; j++)
{
cin>>load;
a[i][j] = load;
}
}
cout<<"请输入入口坐标: "; cin>>R_position.x>>R_position.y;
cout<<"请输入出口坐标: "; cin>>C_position.x>>C_position.y;
cout<<"迷宫创建成功!"<<endl;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
void go_Maze(SqStack &S, int **a)
{
CoordinateDir Dir;
Dir.position.x = R_position.x;
Dir.position.y = R_position.y;
do
{
if(isPass(a, Dir.position) == false)
{
Dir.direction = 1;
Push(S, Dir);
signRoad(a, Dir.position);
if(Dir.position.x == C_position.x && Dir.position.y == C_position.y)
{
break;
}
Dir = isGo(a, Dir);
}
else
{
if(!isEmpty(S))
{
Pop(S, Dir);
while(Dir.direction == 4 && !isEmpty(S))
{
SetWall(a, Dir.position);
Pop(S, Dir);
}
if(Dir.direction < 4)
{
Dir.direction++;
Push(S, Dir);
Dir = isGo(a, Dir);
}
}
}
if(Dir.direction == 4 && isEmpty(S))
{
cout<<"迷宫没有出路!"<<endl;
break;
}
}while(true);
}
void showPath(int **&a, SqStack &S)
{
SqStack s;
InitStack(s);
CoordinateDir Dir;
while(!isEmpty(S))
{
Pop(S, Dir);
Push(s, Dir);
}
while(!isEmpty(s))
{
Pop(s, Dir);
cout<<"(" <<Dir.position.x<<", "<<Dir.position.y<<")";
}
}
int main()
{
SqStack S;
InitStack(S);
int **a = NULL;
create_Maze(a);
go_Maze(S, a);
showPath(a, S);
return 0;
}
此博文只用于博主记录学习过程(有问题可以评论)