資源簡介
供C++初學者參考,代碼沒什么難度,放網上也可以幫幫人,對初學者有幫助的
代碼片段和文件信息
#include
#include
#include
#include
#define?STACK_INIT_SIZE?20
#define?STACKINCREMENT??10
#define?M?10
#define?OVERFLOW?0
#define?ERROR?0
#define?OK?1
typedef?struct?Qlength
{
????int?row;//行坐標
????int?line;//列坐標
}PosType;
typedef?struct
{
????int?ord;//通道塊在路徑上的序號
????PosType?seat;//通道塊在迷宮中的坐標位置
????int?di;//從次通道走向下一通道塊的方向
}SElemType;
typedef?struct
{
????SElemType?*base;
????SElemType?*top;
????int?stacksize;
}SqStack;
int??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;
}
int?push(SqStack?&SSElemType?e)
{
????if(S.top-S.base>=S.stacksize)//棧滿,追加存儲空間
????{
????????S.base=(SElemType*)realloc(S.base(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
????????if(!S.base)?exit(OVERFLOW);
????????S.top=S.base+S.stacksize;
????????S.stacksize+=STACKINCREMENT;
????}
????*S.top++=e;
????return?OK;
}
int?GetTop(SqStack?&SSElemType?e)
{
????if(S.top==S.base)return?ERROR;
????e=*(S.top-1);
????return?OK;
}
int?pop(SqStack?&SSElemType?&a)
{
????if(S.top==S.base)?return?ERROR;
????a=*--S.top;
????return?OK;
}
int?StackEmpty(SqStack?&S)
{
????if(S.top==S.base){
????????return?OK;}
????else
????????return?ERROR;
}
int?Pass(int?maze[M][M]PosType?curpos)//判斷是否通路
{
????if(maze[curpos.row][curpos.line]==0)
????return?OK;
????else
????return?ERROR;
}
int?FootPrint(int??maze[M][M]PosType?curpos)//通路留下標記
{
????return?maze[curpos.row][curpos.line]=2;
}
PosType?NextPos(PosType?curposint?di)//向四個方向依次探索
{
????PosType?curpos1;
?????switch(di)
?????{
?????case?1:
???????????curpos1.row=curpos.row;
???????????curpos1.line=curpos.line+1;
???????????break;
?????case?2:
???????????curpos1.row=curpos.row+1;
???????????curpos1.line=curpos.line;
???????????break;
?????case?3:
???????????curpos1.row=curpos.row;
???????????curpos1.line=curpos.line-1;
???????????break;
?????case?4:
???????????curpos1.row=curpos.row-1;
???????????curpos1.line=curpos.line;
???????????break;
?????}
?????return?curpos1;
}
void??Markprint(int?maze[M][M]PosType?curpos)//死路留下標記
{
????maze[curpos.row][curpos.line]=3;
}
int?MazePath(int??maze[M][M]PosType?startPosType?end1SqStack?&S)//尋路
{
????SElemType?e;
????PosType?curpos;
????curpos=start;//?1?1
????int?cursteps=1;
????do{
????????if(Pass(mazecurpos))//Pass?函數
????????{
???????????FootPrint(mazecurpos);
???????????e.ord=cursteps;
???????????e.di=1;
???????????e.seat=cu
評論
共有 條評論