#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
using namespace std;
int mazeMap[100][100];
typedef struct
{
int x,y;
}PosType;
typedef struct
{
PosType seat;
int di;
}SElemType,MazeType;
typedef struct{
MazeType *base;
MazeType *top;
int stacksize;
}MazeStack;
MazeStack S;
typedef int Status;
Status InitStack(MazeStack &S);
Status StackEmpty(MazeStack &S);
Status Push(MazeStack &S, MazeType &e);
Status GetTop(MazeStack s,MazeType &e);
Status Pop(MazeStack &S, MazeType &e);
Status MazePath(PosType start, PosType end);
Status Pass(PosType &pos);
void FootPrint(PosType pos);
PosType NextPos(PosType curPos, int &i);
void MakePrint(PosType pos);
int Long,wide;
void interface()
{
cout<<"**请选择**********"<<endl;
cout<<"* 1查询 *"<<endl;
cout<<"* 2退出 *"<<endl;
cout<<"******************"<<endl;
}
int main()
{
while(1)
{
//system("cls");
interface();
int cho;
cin>>cho;
if(cho==1)
{
memset(mazeMap,0,sizeof(mazeMap));
cout<<"请输入迷宫大小"<<endl;
cin>>Long>>wide;
for(int i = 0;i <= wide;i++)
{
mazeMap[0][i] = 1;
mazeMap[Long+1][i] = 1;
}
for(int i = 0;i <= Long;i++)
{
mazeMap[i][0] = 1;
mazeMap[i][wide+1] = 1;
}
int num;
cout<<"请输入障碍点个数"<<endl;
cin>>num;
int x,y;
cout<<"请输入障碍点坐标"<<endl;
{
for(int i=0;i<num;i++)
{
cin>>x>>y;
mazeMap[x][y]=1;
}
}
PosType Start, End;
Start.x = 1;
Start.y = 1;
End.x = Long;
End.y = wide;
system("cls");
cout<< "起始点为(1,1)"<<endl;
cout<< "出口为("<<Long<<","<<wide<<")"<<endl;
cout<<"迷宫为:"<<endl;
for(int i=0;i<=Long+1;i++)
{
for(int j=0;j<=wide+1;j++)
cout << mazeMap[i][j];
cout << endl;
}
if(MazePath(Start, End)){
cout << "走通迷宫" << endl;
int pri[1000][2];
int temp=0;
while(!StackEmpty(S)){
MazeType tem;
pri[temp][0]=S.top->seat.x;
pri[temp++][1]=S.top->seat.y;
Pop(S,tem);
}
cout<<"(1,1)";
for(int i=temp-1;i>0;i--)
{
cout<<"->("<<pri[i][0]<<","<<pri[i][1]<<")";
}
cout<<endl;
}
else
cout << "走不通迷宫" << endl;
}
else if(cho==2)
{
break;
}
else{
system("cls");
cout<<"输入错误,请重新输入"<<endl;
}
}
return 0;
}
Status InitStack(MazeStack &S)
{
S.base = (MazeType *)malloc(STACK_INIT_SIZE*sizeof(MazeType));
if(!S.base) exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status Push(MazeStack &S, MazeType &e)
{
if(S.top - S.base >= S.stacksize)
{
S.base = (MazeType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(MazeType));
if(S.base) exit (OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
Status Pop(MazeStack &S, SElemType &e)
{
if(S.top == S.base) return ERROR;
e = * -- S.top;
return OK;
}
Status StackEmpty(MazeStack &S)
{
if(S.base == S.top)
return OK;
return ERROR;
}
Status GetTop(MazeStack S,MazeType &e)
{
if(S.top == S.base) return ERROR;
e = *(S.top-1);
return OK;
}
Status MazePath(PosType Start, PosType End)
{
PosType curpos;
MazeType e;
InitStack(S);
curpos = Start;
do
{
if(Pass(curpos)==1)
{
FootPrint(curpos);
e.seat = curpos;
e.di = 1;
Push(S, e);
if(curpos.x == End.x && curpos.y == End.y)
{
return TRUE;
}
curpos = NextPos(curpos, e.di);
}
else
{
if(!StackEmpty(S))
{
Pop(S, e);
while(e.di == 4 && !StackEmpty(S))
{
MakePrint(e.seat);
Pop(S, e);
}
if(e.di < 4)
{
++e.di;
Push(S, e);
curpos = NextPos(e.seat, e.di);
}
}
}
}while(!StackEmpty(S));
return FALSE;
}
Status Pass(PosType &pos)
{
if(mazeMap[pos.x][pos.y] == 1)
{
return 0;
}
else{
return 1;
}
}
void FootPrint(PosType pos)
{
mazeMap[pos.x][pos.y] = -1;
}
PosType NextPos(PosType curPos, int &i)
{
switch(i)
{
case 1:
++curPos.x;
if(mazeMap[curPos.x][curPos.y] !=-1)
break;
--curPos.x;
case 2:
i = 2;
++curPos.y;
if(mazeMap[curPos.x][curPos.y] != -1)
break;
--curPos.y;
case 3:
i = 3;
--curPos.x;
if(mazeMap[curPos.x][curPos.y] != -1)
break;
++curPos.x;
case 4:
i = 4;
--curPos.y;
if(mazeMap[curPos.x][curPos.y] == -1)
{
++curPos.y;
mazeMap[curPos.x][curPos.y] =1;
}
break;
}
return curPos;
}
void MakePrint(PosType pos)
{
mazeMap[pos.x][pos.y] = 1;
}