数据结构--迷宫

迷宫游戏的代码
     主要方法,运用栈的思想

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;
}


/* ****迷宫算法求解************* */ /**计目标:教学演示**************/ #include #define rows 10 #define cols 10 typedef struct {int row; int col; }PosType;/* //坐标点结构 */ typedef struct {int ord;/*//通道块在路径上的“序号” */ PosType seat;/*//通道块在迷宫中的“坐标位置”*/ int di;/*//从此通道快走向下一通道块的“方向” */ }SElemType;/*//栈的元素类型 */ typedef struct {SElemType *base; SElemType *top; int stacksize; }SqStack;/*//堆栈结构 */ void InitStack(SqStack &s)/*//初始化堆栈 */ { s.base=(SElemType *)malloc(100*sizeof(SElemType)); if(!s.base) return NULL; s.top=s.base; s.stacksize=100; } int StackEmpty(SqStack s)/* //栈空判别*/ {return(s.top==s.base); } void Pop(SqStack &s ,SelemType &e)/*//弹栈 */ {e=*--s.top); } void Push(SqStack &s,SElemType e)/*//将元素压入堆栈*/ { *s.top++=e; } /*static int maze[rows][cols]= {{0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,0,1,0,1,0}, {0,1,1,0,1,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,0,1,1,0}, {0,1,0,0,0,1,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, }; */ /* //初始迷宫数据(1-通,0-不通)*/ static int maze[rows][cols]= {{0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,0,1,1,1,0}, {0,1,1,1,0,0,0,0,1,0}, {0,1,0,0,0,1,1,1,1,0}, {0,1,0,1,0,1,0,0,0,0}, {0,1,0,1,1,1,0,1,1,0}, {0,1,0,1,0,0,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, }; /* //初始迷宫数据(1-通,0-不通)*/ static int foot[10][10]={0};/*//标记某点是否走过(1-走过,0-未走过)*/ void printpath(SqStack &s)/*//打印迷宫通路*/ {int i,j; SElemType e; while(!StackEmpty(s)) { Pop(s,e); foot[e.seat.row][e.seat.col]=1; } for(i=0;i<10;i++) {printf("\n"); for(j=0;j<10;j++) if(foot[i][j]) printf(" # "); else printf(" . "); } } int Pass(PosType pos)/*//判断当前的通道块是否可通*/ { return(maze[pos.row][pos.col]); }; void FootPrint(PosType pos) { maze[pos.row][pos.col]=0; } PosType NextPos(PosType curpos,int dir)/*//取当前通道块的下一个通道块*/ { switch(dir) {case 1: curpos.row++; break; case 2: curpos.col++; break; case 3: curpos.row--; break; case 4: curpos.col--; } return curpos;/*//将下一个通道块变为当前通道块*/ } int END(PosType curpos,PosType end) {return(curpos.row==end.row && curpos.col==end.col); } void MazePath(SqStack &s,PosType start,PosType end) {PosType curpos,nextpos; int curstep; SElemType e; SqStack *s; s=InitStack(); curpos=start; curstep=1; do{ if(Pass(curpos)) {FootPrint(curpos); e.ord=curstep;e.seat=curpos;e.di=1; Push(s,e); if(END(curpos,end)) return s; curpos=NextPos(curpos,1); curstep++; }/* end of if */ else { if(!StackEmpty(s)) { e=Pop(s); while(e.di==4 && !StackEmpty(s)) {FootPrint(e.seat);/* The same fuction as MarkPrint ? */ e=Pop(s); }/* end of while */ if(e.di<4) {e.di++;Push(s,e); curpos=NextPos(e.seat,e.di); } /* end of if */ } /* end of if */ } /* end of else */ }while(!StackEmpty(s)); curstep=0; return NULL; } void main() {SqStack *s; static PosType start={1,1},end={8,8}; s=MazePath(start,end); if(s) printpath(s); else printf("\n NO find the path!"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值