迷宫游戏的代码
主要方法,运用栈的思想
Stack 头文件
#include <stdlib.h>
//#define SElemType int
#define INIT_STACK_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define INFEASIBLE -1
typedef struct SElemType
{
int ord;
int maze_pos;
int dir;
}SElemType;
typedef struct SqStack//定义栈的结构
{
SElemType *top;
SElemType *base;
int stacksize;
}SqStack;
int InitStack(SqStack &S)//初始化栈
{
S.base = (SElemType*)malloc(INIT_STACK_SIZE * sizeof(SElemType));
if(!S.base)
/*exit(OVERFLOW);*/
return ERROR;
S.top = S.base;
S.stacksize = INIT_STACK_SIZE;
return OK;
}
int GetTop(SqStack &S, SElemType &e)//获得栈顶元素
{
if(S.base == S.top)
return ERROR;
e = *(S.top-1);
return OK;
}
int Push(SqStack &S, SElemType e)//压栈
{
if((S.top - S.base) >= INIT_STACK_SIZE)
{
S.base = (SElemType*)realloc(S.base,(STACKINCREMENT+INIT_STACK_SIZE)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*(S.top++) = e;
return OK;
}
int Pop(SqStack &S, SElemType &e)//退栈
{
if(S.base == S.top)
return ERROR;
e = *(--S.top);
return OK;
}
int DestroyStack(SqStack &S)//销毁栈
{
S.base = NULL;
S.top = NULL;
S.stacksize = 0;
return OK;
}
int ClearStack(SqStack &S)//清空栈
{
if(InitStack(S))
return OK;
else
return ERROR;
}
int StackEmpty(SqStack &S)//判断栈是否空
{
if(S.top == S.base)
return OK;
else
return ERROR;
}
int StackLength(SqStack &S)//栈中元素的个数
{
return (S.top - S.base);
}源文件
// Maze.cpp : Defines the entry point for the console application.
/*-----CODE FOR FUN---------------
-------CREATED BY Dream_Whui--
-------2015-1-21--------------------*/
#include "stdafx.h"
#include "Stack.h"
#include <iostream>
using namespace std;
#define LENGTH 100
typedef struct FootPrint
{
int data[LENGTH];
int length;
}FootPrint;
FootPrint Foot;
FootPrint Mask;
SqStack S;
int Maze[LENGTH]={0,0,0,0,0, 0,0,0,0,0,
0,1,1,0,1, 1,1,0,1,0,
0,1,0,0,0, 1,1,0,1,0,
0,1,0,1,0, 0,0,1,1,0,
0,1,1,0,0, 1,1,1,1,0,
0,1,1,1,0, 1,1,1,1,0,
0,1,0,1,1, 1,1,0,0,0,
0,1,0,0,0, 0,1,0,0,1,
0,0,1,1,1, 1,1,1,1,0,
0,0,0,0,0, 0,0,0,0,0};
void LeaveFoot(FootPrint &F, int curpos)
{
F.data[F.length] = curpos;
F.length++;
}
int NextPos(int curpos, int dir)
{
switch(dir)
{
case 1:
curpos += 1;
break;
case 2:
curpos += 10;
break;
case 3:
curpos -= 1;
break;
case 4:
curpos -= 10;
break;
}
return curpos;
}
int Find_In_Foot(FootPrint &Foot, int curpos)
{
for(int i=0; i<Foot.length; i++)
{
if(Foot.data[i] == curpos)
return 0;//已在历史路径上或者不通路径
}
return 1;//不在历史路径上和不通路径上
}
int Pass(int curpos)
{
if(curpos < 0)
return 0;
if(curpos > LENGTH)
return 0;
if(Maze[curpos]==0)
return 0;
if(Find_In_Foot(Foot,curpos)==0)
return 0;
if(Find_In_Foot(Mask,curpos)==0)
return 0;
return 1;
}
int MazePath(int *Maze, int start ,int end)
{
InitStack(S);
int curpos = start;//Maze位置
int curstep = 1;//路径
SElemType e;
do
{
if(Pass(curpos))
{
LeaveFoot(Foot,curpos);//留下记录
e.ord = curstep;
e.maze_pos = curpos;
e.dir = 1;
Push(S,e);
if(curpos == end)
return 1;
curpos = NextPos(curpos,1);//下一个位置
curstep++;
}
else
{
if(!StackEmpty(S))
{
Pop(S,e);
while(e.dir==4 && !StackEmpty(S))
{
LeaveFoot(Mask,e.maze_pos);//不能通过该位置
Pop(S,e);
}
if(e.dir<4)
{
e.dir++;
Push(S,e);
curpos = NextPos(e.maze_pos,e.dir);
}
}
}
}while(!StackEmpty(S));
return 0;
}
int main(int argc, char* argv[])
{
SElemType e;
if(MazePath(Maze,11,88))
{
while(!StackEmpty(S))
{
Pop(S,e);
cout<<e.maze_pos<<" ";
}
cout<<endl;
}
return 0;
}
1万+

被折叠的 条评论
为什么被折叠?



