#define maxsize 100 //maxsize为栈的最大值
#define m 10
#define n 10
#include <iostream>
using namespace std;
//定义所在位置的类型
struct place
{
int x;
int y;
int d;//所在位置的横纵坐标及下一步的通行方向
};
struct SqStack
{
place data[maxsize];
int top; //栈顶指针
};
//将迷宫转化为数组,以“1”表示不能通过,“0”表示能通过
int Maze[10][10]=
{
{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 Move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
//栈的初始化函数
void StackInit(SqStack s)
{
s.top=-1;
}
//入栈函数
bool Push_SqStack(SqStack &s,place t)
{
if(s.top==maxsize-1) return false;
else {
s.top++;
s.data[s.top]=t;
return true;
}
}
//出栈函数
bool Pop_SqStack(SqStack &s,place t)
{
if(s.top==-1) return false;
else {
t=s.data[s.top];
s.top--;
return true;
}
}
//判断栈是否为空的函数
bool StackEmpty(SqStack s)
{
if (s.top==-1) return true;
else return false;
}
//迷宫求解函数
int path(SqStack s,int maze[][10],int move[][2]) //maze为迷宫二维数组名,m和n分别为一、二维长度;move为表示移动的二维数组,存放了试探的4个方向坐标
{
place temp;
int x,y,d,i,j;
temp.x=1;temp.y=1;temp.d=-1;
Push_SqStack(s,temp);
while(!StackEmpty(s)){
Pop_SqStack(s,temp);
x=temp.x;
y=temp.y;
d=temp.d+1;
while(d<4){
i=x+move[d][0];
j=y+move[d][1];
if(maze[i][j]==0){
temp.x=x; temp.y=y; temp.d=d;
Push_SqStack(s,temp);
x=i;y=j;maze[x][y]=-1;
if(x==m&&y==n) return 1; //迷宫有路
else d=0;}
else d++;}
}
return 0; //当栈为空栈时,则迷宫无路
}
//主函数
void main()
{
SqStack s;
StackInit(s);
int k=0,l,a;
a=path(s,Maze,Move);
if(a==0) cout<<"迷宫无路";
if(a==1) cout<<"迷宫有路且路径如下:"<<endl;
while(!StackEmpty(s))
{
cout<<"("<<s.data[s.top].x<<","<<s.data[s.top].y<<","<<s.data[s.top].d<<") ";
k++;
s.top--;
if(k%5==0) cout<<endl;
}
cin>>l;
}
error C2143: 语法错误 : 缺少“)”(在“常量”的前面)
error C2143: 语法错误 : 缺少“;”(在“常量”的前面)
error C2059: 语法错误:“)”
error C2143: 语法错误 : 缺少“)”(在“常量”的前面)
error C2143: 语法错误 : 缺少“;”(在“常量”的前面)
error C2059: 语法错误:“)”