这......................................................................
我也是醉了
看不太懂,大神们求解.............................................
#include<iostream>
#include<stack>
#include<stdio.h>
#include<time.h>
#include<string>
using namespace std;
typedef struct
{
int x,y;
}item; typedef struct
{
int x,y,d;
}Datetype;
typedef stack<Datetype> stack_int;
void path (int **maze,int,int,int,int);
void printpath();
#define NUM 100 //队列大小;
typedef struct{
int x,y; //所到点的坐标;
int pre; //前驱点的下标;
}SqType; //队列;
int front,rear; //队首指针与队尾指针;
void printpath(SqType sq[],int){//打印路径
int i;
i=rear;
do{
cout<<"("<<sq[i].x<<","<<sq[i].y<<")<--";
i=sq[i].pre; //回溯;
}while(i!=-);
}
void restore(int **maze,int m,int n){//恢复迷宫
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
if(maze[i][j]==-);
}
}
}
int path1(int **maze,int m,int n,int c,int d,int x1,int y1)//最短路径
{ //m,n为迷宫的长和宽,c,d为迷宫入口坐标,x1,y1为迷宫出口坐标;maze为迷宫;
item move[]={{,},{,},{,},{,-},{,-},{-,-},{-,},{-,}}; //坐标增量数组;
SqType sq[NUM];
int x,y,i,j,v;
front=rear=;
sq[].x=c;
sq[].y=d;
sq[].pre=-;
if(maze[c][d]==)
maze[c][d]=-;//入口点入队;
else goto G;
while(front<=rear){ //队列不为空
x=sq[front].x;
y=sq[front].y;
for(v=;v<;v++){
i=x+move[v].x;
j=y+move[v].y;
if(maze[i][j]==){
rear++;
sq[rear].x=i;
sq[rear].y=j;
sq[rear].pre=front;
maze[i][j]=-; //访问过的坐标点,入队;
}
if(i==x1&&j==y1){
cout<<"最短路径为:"<<endl;
printpath(sq,rear); //输出路径;
restore(maze,m,n); //恢复迷宫;
return ;
}
} //for v;
front++; //当前点搜索完,取下一个点搜索
} //while
G:cout<<"无路径。"<<endl;
return ;
}
void path(int **maze,int a,int b,int m,int n)
{
item move[]={{,},{,},{,},{,-},{,-},{-,-},{-,},{-,}};
stack_int st;
Datetype temp;
int x,y,d,i,j;
if(maze[a][b]==){cout<<"进口输入有误。";return;}
temp.x=a;temp.y=b;temp.d=-; //初始化入口点坐标及方向;
st.push(temp);
while(!st.empty())
{
temp=st.top();
st.pop();
x=temp.x;y=temp.y;d=temp.d+;
while(d<)
{
i=x+move[d].x;j=y+move[d].y;
if(maze[i][j]==) //该点可到达;
{
temp.x=x;temp.y=y;temp.d=d; //坐标及方向;
st.push(temp); //坐标及方向入栈;
x=i;y=j;
maze[x][y]=-;//到达新点;
if(x==m && y==n)
{
cout<<" 迷宫路径为:"<<endl;
cout<<"("<<m<<","<<n<<")<---";
Datetype t;
while(!st.empty())
{
t=st.top();
cout<<"("<<t.x<<","<<t.y<<")<---";
st.pop();
} //输出路径;
cout<<endl;
return ; //到达出口;
}
else d=; //重新初始化方向;
}
else d++; //改变方向;
}
}
cout<<"对不起,无法找到出口.";
return; //迷宫无路;
} void printpath()
{
int m,n,i,j,l,c,d;
string s;
cout<<"********************************************************************************\n"<<endl;
cout<<" 欢迎进入迷宫求解系统\n"<<endl;
cout<<"********************************************************************************\n"<<endl;
cout<<" 请输入迷宫的行数:"<<endl;
cin>>m;
cout<<" 请输入迷宫的列数:"<<endl;
cin>>n;
int **maze=new int*[m+];
for(i=;i<=m+;i++)
maze[i]=new int[n+];//申请迷宫的空间;
for(i=;i<=m+;i++)
maze[i][]=;
for(i=;i<=n+;i++)
maze[][i]=;
for(i=;i<=m+;i++)
maze[i][n+]=;
for(i=;i<=n+;i++)
maze[m+][i]=; //建立迷宫周围的墙;
cout<<"********************************************************************************\n"<<endl;
cout<<" ☆ 自动生成迷宫 请按:1\n ☆ 手动生成迷宫 请按:2\n"<<endl;
cout<<"********************************************************************************\n"<<endl;
cin>>s;
if(s=="")
{
srand(time()); //系统时间随机函数;
for(i=;i<=m;i++)
for(j=;j<=n;j++)
maze[i][j]=rand()%; //随机赋值
maze[][]=; //(1,1)点为可通过点;
maze[m][n]=; //(m,n)点为可通过点;
}
else
{
cout<<"请输入迷宫:"<<m<<"行"<<n<<"列"<<", 输入必须为'0' 或 '1';"<<endl;
for(i=;i<=m;i++) //输入第i行迷宫的构造;
for(j=;j<=n;j++) //输入第j列迷宫的结构;
{
cin>>maze[i][j];
A:if(maze[i][j]!= && maze[i][j]!=)
{
cout<<"请再次输入:";
cin>>maze[i][j];goto A; //判错;
}
}
}
cout<<"迷宫如下:"<<endl; //显示用户输入的迷宫;
for(i=;i<=m+;i++)
{
for(j=;j<=n+;j++)
{
if (*(maze[i]+j)==||*(maze[i]+j)==)
{
cout<<" ";
}
else if(*(maze[i]+j)==)
{
cout<<"□";
}
else if(*(maze[i]+j)==)
{
cout<<"※";
}
else if(*(maze[i]+j)==)
{
cout<<"■";
}
else if(*(maze[i]+j)==)
{
cout<<"☆";
}
else if(*(maze[i]+j)==) //找到出口标识
{
cout<<"★";
} else
{
cout<<"出错!";
} }
cout<<"\n";
}
H:cout<<"请输入迷宫入口(a,b),出口(c,d):";
cin>>i>>j>>c>>d;
path(maze,i,j,c,d); //调用路径函数,输出路径;
cout<<endl; //格式设置;
restore(maze,m,n); //恢复迷宫;
path1(maze,m,n,i,j,c,d); //输出最短路径;
cout<<endl; //格式设置;
cout<<"********************************************************************************\n";
cout<<" 1、寻找其他入口与出口;\n 2、退出此迷宫;\n";
cout<<"********************************************************************************\n"<<endl;
cin>>l;
if(l==){
restore(maze,m,n); //恢复迷宫;
goto H;
}
else return; //跳出此函数;
}
int main()
{
string s="Y";
do
{
cout<<"--------------------------欢迎到达迷宫界面--------------------------\n";
printpath();
cout<<endl;
cout<<"\n 是否继续?'Y' 或'N'(输入其他操作按'N')"<<endl;
cin>>s;
}while (s=="Y"||s=="y");
system("pause");
}
这...........迷宫代码求改善.............