迷宮求解問題——堆棧的使用

代碼:

queue.h

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define Status bool
#define ERROR 0
#define OVERFLOW -2
#define OK 1

 struct PosType
 {
int x;
int y;
};

 typedef struct 
 {
int ord; //通道塊在路徑上的序號
PosType seat; //通道塊在路徑中的位置
int di; //從次通道塊通往下一個通道塊的方向1=東 2=南 3=西 4=北
 }SElemType;

typedef struct  
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

Status InitStack(SqStack &s);

Status DestoryStack(SqStack &s);

Status StackEmpty(SqStack s);

int StackLength(SqStack s);

Status GetTop(SqStack s, SElemType &e);

Status Push(SqStack &s, SElemType e);

Status Pop(SqStack &s, SElemType &e);

////////////////////////////////////////////////

queue.cpp

#include <stdlib.h>
#include "queue.h"

Status InitStack(SqStack &s)
{
s.base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if (!s.base) exit(OVERFLOW);

s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return OK;
}

Status DestoryStack(SqStack &s)
{
if ( s.base == s.top)
{
return ERROR;
}
SElemType e;
for( ; s.base != s.top; )
{
Pop(s, e);
}
return OK;
}


Status StackEmpty(SqStack s)
{
if (s.base != s.top)
{
return false;
}
return true;
}

int StackLength(SqStack s)
{
int i = 0;
SElemType *p = s.top;
if (s.base == s.top) return 0;
for (; s.base != p; )
{
--p;
i++;
}
return i;
}

Status GetTop(SqStack s, SElemType &e)
{
if (s.base == s.top) return ERROR;
e = *(s.top - 1);
return OK;
}

Status Push(SqStack &s, SElemType e)
{
if (s.top - s.base >= s.stacksize)
{
s.base = (SElemType*)realloc(s.base, (STACK_INIT_SIZE + STACKINCREMENT)*sizeof(SElemType));
if (!s.base) exit(OVERFLOW);

s.top = s.base + s.stacksize;
s.stacksize += STACKINCREMENT;
}
*s.top++ = e;
return OK;
}

Status Pop(SqStack &s, SElemType &e)
{
if (s.base == s.top) return ERROR;
e = *--s.top;
return OK;
}
//////////////////////////////////////////////////

main.cpp

#include<iostream.h>
#include "queue.h"
#define  MazeType int

//迷宮數組,0表示可以通過,1表示不能通過
int Array[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},


};


//棧////////////////////////////////////
/* postype 1=東 2=南 3=西 4=北*/

SqStack s;
PosType START = {1, 1};
PosType END = {8, 8};
//START.x = 1;
//START.y = 1;
//END.x = 8;
//END.y = 8;

Status Pass(PosType &pos);
void FootPrint(PosType &pos);
PosType NextPos(PosType &pos, int direction);
void MarkPrint(PosType &pos);
Status  MazePath(MazeType maze, PosType start, PosType end);
void OutPut();

void main()
{


for(int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (Array[i][j] == 0)
{
if(i==1 && j==1)
cout<<" "<<"S"<<" ";
else if(i==8 && j==8)
cout<<" "<<"E"<<" ";
else
cout<<" "<<"  ";
}
else
cout<<"|"<<Array[i][j]<<"|";
}
cout<<endl;
}

bool isPass = MazePath(1, START, END);
if (isPass ) 
{
cout<<"Maze is Pass!!!\nthe path is :\n ";
OutPut();
}
else
{
cout<<"Maze is CAN NOT Pass!!! "<<endl;
}
}


Status Pass(PosType &pos)
{
if (!Array[pos.x][pos.y])
{
return true;
}
return false;
}

void FootPrint(PosType &pos)
{
Array[pos.x][pos.y] = 2; //可以通過
}

PosType NextPos(PosType &pos, int direction)
{
switch(direction)
{
//東
case 1:
pos.y++;
return pos;
//南
case 2:
pos.x ++;
return pos;
//西
case 3:
pos.y --;
return pos;
//北
case 4:
pos.x --;
return pos;
default:
return pos;
}
}

void MarkPrint(PosType &pos)
{
//if (Pass(pos))
{
Array[pos.x][pos.y] = -2; //不可以通過
}
}

Status  MazePath(MazeType maze, PosType start, PosType end)
{

PosType curpos;
int  curstep;
SElemType e;
InitStack(s);
curpos = start;
curstep = 1;

do 
{
if (Pass(curpos))
{
FootPrint(curpos);


e.ord = curstep;
e.seat.x = curpos.x;
e.seat.y = curpos.y;
e.di = 1;
Push(s, e);


if ((curpos.x == end.x) &&(curpos.y == end.y)) return true;

curpos = NextPos(curpos, 1);
curstep ++;
}
else
{
if (!StackEmpty(s))
{
Pop(s, e);
//cout<<"curstep = "<<curstep<<"---"<<"e = {"<<e.ord<<",("<<e.seat.x<<","<<e.seat.y<<",)"<<e.di<<"}"<<endl;
while(e.di == 4 && !StackEmpty(s))
{
//不能通過
MarkPrint(e.seat);
Pop(s, e);
//cout<<"curstep = "<<curstep<<"---"<<"e = {"<<e.ord<<",("<<e.seat.x<<","<<e.seat.y<<",)"<<e.di<<"}"<<endl;
}
}
if (e.di < 4 )
{
e.di ++;
Push(s, e);
curpos = NextPos(e.seat, e.di);
}
}
} while (!StackEmpty(s));


return false;
}


void OutPut()
{
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
if ((Array[x][y] == 0) || (Array[x][y] ==-2))
{
cout<<" "<<"  ";
}
else if ((Array[x][y] !=1) && (Array[x][y] !=-2))
{
//此處爲路徑
//cout<<"Array["<<x<<"]["<<y<<"] = "<<Array[x][y]<<endl;
if(x==1 && y==1)
cout<<" "<<"S"<<" ";
else if(x==8 && y==8)
cout<<" "<<"E"<<" ";
else
cout<<" "<<"0"<<" ";
}
else 
cout<<"|"<<Array[x][y]<<"|";
}
cout<<endl;
}
}

運行結果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章