MagicPath

#include<iostream>
#include<malloc.h>
using namespace std;

#define ElemType int 
#define MaxSize 100
#define M 8
#define N 8
int mg[M+2][N+2] =
{
{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},
};
typedef struct
{
int i;//当前方块的行数
int j;//当前方块的列数
int di;//下一相邻可走方位的方位号
}Box;

typedef struct
{
Box data[MaxSize];
int top;
}StType;//顺序栈类型

void InitSqStack(StType *&S)
{
S = (StType *)malloc(sizeof(StType));
S ->top = -1;
}

void DestorySqStack(StType *&S)
{
free(S);
}

bool SqStackEmpty(StType *S)
{
return (S->top == -1);
}

bool Push(StType*&S,Box e)
{
if( S->top == MaxSize -1)
{
return false;
}
else
{
S->top ++;
S->data[S->top] = e;
return true;
}
}


bool Pop(StType*&S,Box &e)
{
if(S->top == -1)
{
return false;
}
else
{
e = S->data[S->top];
S->top--;
return true;
}
}


bool GetTop(StType *S,Box &e)
{
if(S->top == -1)
{
return false;
}
else
{
e = S->data[S->top];
return true;
}
}

bool MgPath(int xi,int yi,int xe,int ye)
{
Box path[MaxSize];
Box e;

int i,j,di,i1,j1,k;
bool find;
StType *st;//定义栈
InitSqStack(st);
e.i = xi;
e.j = yi;
e.di = -1;//e为入口

Push(st,e);//e进栈
mg[xi][yi] = -1;//入口置为-1
while(!SqStackEmpty(st))
{
GetTop(st,e);
i = e.i;
j = e.j;
di = e.di;
if(i == xe && j == ye)//找到出口
{
cout<<"One path "<<endl;
k = 0;
while(!SqStackEmpty(st))
{
Pop(st,e);//输出e
path[k++] = e;//进path中
}
while(k >= 1)
{
k--;
cout<<"("<<path[k].i<<","<<path[k].j<<")";
if((k +2 ) % 5 == 0)
{
cout<<endl;
}
}
cout<<endl;
DestorySqStack(st);
return true;
}
find = false;//找方块(i,j)的下一个相邻可走方块(i1,j1)
while(di < 4 && !find)
{
di++;
switch(di)
{
case 0:
i1 = i-1;
j1 = j;
break;
case 1:
i1 = i;
j1 = j+1;
break;
case 2:
i1 = i+1;
j1 = j;
break;
case 3:
i1 = i;
j1 = j-1;
break;
}
if(mg[i1][j1]==0)
{
find = true;//找到
}
}
if(find)//找到相邻可走方块(i1,j1)
{
st->data[st->top].di = di;//修改栈顶元素的di值
e.i = i1;
e.j = j1;
e.di = -1;
Push(st,e);//相邻可走方块e进栈
mg[i1][j1] = -1;//(i1,j1)迷宫值置为-1,避免重复走
}
else//无路
{
Pop(st,e);//栈顶退栈
mg[e.i][e.j] = 0;//位置置为其他路径可走方块
}
} 
DestorySqStack(st);
return false;
}


typedef struct 
{
int i,j;//方块位置
int pre;//本路径上一个方块在队列中的下标
}Box2;
typedef struct
{
Box2 data[MaxSize];
int front,rear;
}QuType;

void InitQueue(QuType *&q)
{
q =(QuType*)malloc(sizeof(QuType));
q->front = q->rear = 0;
}
void DestoryQueue(QuType *&q)
{
free(q);
}
bool QueueEmpty(QuType *q)
{
return (q->front == q->rear);
}

bool enQueue(QuType *&q,Box2 e)
{
if((q->rear+1) % MaxSize == q->front)//队满
{
return false;
} 
else
{
q->rear = (q->rear+1) % MaxSize;
q->data[q->rear] = e;
return true;
}
}

bool deQueue(QuType *&q,Box2 &e)
{
if(q->front == q->rear)//队空 
{
return false;
}
else
{
q->front = (q->front+1) % MaxSize;
e = q->data[q->front];
return true;
}
}
void Cout(QuType *qu,int front)
{
int k = front,j,ns = 0;
cout<<endl;
do//反向查找最短路径,将该路径上的方块的pre成员置为-1
{
j = k;
k = qu->data[k].pre;
qu->data[j].pre = -1;
}while( k != 0);

cout<<"One Path"<<endl;
k = 0;
while(k < MaxSize)//正向搜索到pee为-1的方块
{
if(qu->data[k].pre == -1)
{
ns++;
cout<<"("<<qu->data[k].i<<","<<qu->data[k].j<<")";
if(ns % 5 ==0)
{
cout<<endl;
}
}
k++;
}
cout<<endl;
}
bool MgPath2(int xi,int yi,int xe,int ye)
{
Box2 e;
int i,j,di,i1,j1;
QuType *qu;
InitQueue(qu);
e.i = xi;
e.j = yi;
e.pre = -1;
enQueue(qu,e);//(xi,yi)进队
mg[xi][yi] = -1;
while(!QueueEmpty(qu))
{
deQueue(qu,e);//出队e,不循环,仍存在
i = e.i;
j = e.j;
if(i == xe && j == ye)//找到
{
Cout(qu,qu->front);
DestoryQueue(qu);
return true;
}
for(di = 0;di<4;di++)
{
switch(di)
{
case 0:
i1 = i-1;
j1 = j;
break;
case 1:
i1 = i;
j1 = j+1;
break;
case 2:
i1 = i+1;
j1 = j;
break;
case 3:
i1 = i;
j1 = j-1;
break;
}
if(mg[i1][j1] == 0)
{
e.i = i1;
e.j = j1;
e.pre = qu->front;//指向路径中上一个方块的下标
enQueue(qu,e);//(i1,j1)进队
mg[i1][j1] = -1;//避免被重复搜索
}
}
}
DestoryQueue(qu);
return false;
}

 

int main()
{
/*if(!MgPath(1,1,M,N))
{
cout<<"NO way!!"<<endl;
}
*/
if(!MgPath2(1,1,M,N))
{
cout<<"No way!!"<<endl;
}
return 1;
}

  

posted @ 2018-11-17 12:28  回忆酿的甜  阅读(288)  评论(0)    收藏  举报
Live2D