您的位置:首页 > 理论基础 > 数据结构算法

数据结构----迷宫问题

2015-01-10 22:25 204 查看
迷宫问题总共包三个头文件函数(Stack.h,Maze.h,AFXSTD.H),两个cpp函数和一个主函数(main),下面分次介绍出来如何实现及测试后数据和通路。

Stcak.h(栈的基本操作函数声明)

#define STACK_INIT_SIZE 100
#define STACKINCREMENT    10
//------------  栈的顺序存储实现  ------------------------------
typedef struct
{
int row; //迷宫中的行
int col; //迷宫中的列
}PosType;//坐标(row,col)
typedef struct
{
int step;    //当前位置在路径上的"序号"
PosType seat;    //当前的坐标位置
DirectiveType  di;        //往下一个坐标位置的方向
}SElemType;//栈的元素类型
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &s);
//栈的创建,即构造一个空栈
Status GetTop(SqStack s,SElemType &e);
//如栈不为空,用e返回栈顶元素
Status Push(SqStack &s, SElemType e);
//在栈中插入元素
Status Pop(SqStack &s, SElemType &e);
//删除栈顶元素
Status StackEmpty(SqStack s);
//判断栈为不为空
Status DestoryStack(SqStack &s);
//销毁栈


Maze.h(迷宫的建立所需函数声明)

#define ROW 10        //迷宫的行数
#define COL 10        //迷宫的列数
#define Status int
typedef struct
{
int m,n;
int arr[RANGE][RANGE];
}MazeType;
Status InitMaze(MazeType &maze, int a[ROW][COL], int row, int col);
Status Pass(MazeType maze,PosType curpos);
//判断能否通过
Status FootPrint(MazeType &maze,PosType curpos);
//留下足迹
Status MarkPrint(MazeType &maze,PosType curpos);
//留下不能通过的标记
//SElemType CreateSElem(int step, PosType pos, int di);
PosType NextPos(PosType curpos, DirectiveType di);//curpos当前位置
//返回当前节点的下一节点
Status PosEqual(PosType pos1, PosType pos2);
//判断两节点是否相等
void PrintMaze(MazeType maze,int row,int col);
Status MazePath(MazeType &maze,PosType start, PosType end);
//求解一条路径


AFXSTD.H(一些头文件)

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define    TRUE    1
#define    FALSE    0
#define    OK        1
#define    ERROR    0
#define OVERFLOW    -2
typedef int   Status;                //函数的返回值
typedef int DirectiveType;        //下一个通道方向
#define RANGE    100            //迷宫大小
Stack.cpp(栈功能的实现)

#include "AFXSTD.H"
#include "Stack.h"
#include "Maze.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;
}//InitStack
Status GetTop(SqStack s, SElemType &e )
{
if( s.top == s.base) return ERROR;
e = *(s.top-1);
return OK;
}//GetTop
Status Push(SqStack &s, SElemType 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;
}//Push
Status Pop(SqStack &s, SElemType &e)
{
if(s.top==s.base)
return ERROR;
e = * --s.top;
return OK;
}//Pop
Status StackEmpty(SqStack s)
{
return s.base == s.top;
}//StackEmpty
Status DestoryStack(SqStack &s)
{
free(&s);
return OK;
}
Maze.cpp(迷宫的函数实现)

#include "AFXSTD.H"
#include "Stack.h"
#include "Maze.h"
Status InitMaze(MazeType &maze, int a[][COL], int row, int col)
{
//设置迷宫maze的初值,包括加上边缘一圈的值
for(int i=1;i<=row;i++)
{
for(int j=1;j<=col;j++)
{
maze.arr[i][j] = a[i-1][j-1];
}
}
//加上围墙
for(int j=0;j<=col+1;j++)
{
maze.arr[0][j] = maze.arr[row+1][j]=1;
}
for(i=0;i<=row+1;i++)
{
maze.arr[i][0] = maze.arr[i][col+1]=1;
}
maze.m = row, maze.n = col;
return OK;
}//InitMaze
Status Pass(MazeType maze,PosType curpos)
{ //判断当前节点是否通过
if(maze.arr[curpos.row][curpos.col] == 0)
return 1;
else return 0;
}//Pass(Maze
Status FootPrint(MazeType &maze,PosType curpos)
{ //留下足迹
maze.arr[curpos.row][curpos.col]=2;//走过且走得通
return OK;
}//FootPrint
Status MarkPrint(MazeType &maze,PosType curpos)
{//留下不能通过的标记
maze.arr[curpos.row][curpos.col]=3;  //走过但走不通
return OK;
}//MarkPrint
SElemType CreateSElem(int step, PosType pos, int di)
{
SElemType e;
e.step = step;
e.seat = pos;
e.di = di;
return e;
}//CreateSElem
PosType NextPos(PosType curpos, DirectiveType di)//curpos当前位置
{//返回当前节点的下一节点
PosType pos = curpos;
switch(di)
{
case 1:        //东
pos.col++;
break;
case 2:        //南
pos.row++;
break;
case 3:        //西
pos.col--;
break;
case 4:        //北
pos.row--;
break;
}
return pos;
}//NextPos
Status PosEqual(PosType pos1, PosType pos2)
{//判断是不是出口
if(pos1.row==pos2.row && pos1.col==pos2.col)
return 1;
else
return 0;
}//PosEquare
void PrintMaze(MazeType maze,int row,int col)
{//打印路径
int i,j;
printf("  ");
for(i=0;i<=COL+1;i++)//打印列数名
printf("-",i);
printf("\n");
for(i=0;i<=row+1;i++)
{
printf("%d  ",i);//打印行数名
for(j=0;j<=col+1;j++)
{
switch(maze.arr[i][j])
{
case 0:
printf("  ");//没走过,但是通路
break;
case 2:
printf("* ");//走过且走得通
break;
case 3:
printf("@ ");//走过但走不通
break;
case 1:
printf("# ");//障碍
break;
// default: break;
}
}
printf("\n");
}
}//PrintMaze
Status MazePath(MazeType &maze,PosType start, PosType end)
{ //求解迷宫maze中,从入口start到出口end的一条路径
SqStack s; SElemType e;
InitStack(s);
PosType curpos = start;
int curstep = 1;                //探索第一步
do{
if( Pass(maze,curpos) )
{    //如果当前位置可以通过,即是未曾走到的通道块
FootPrint(maze,curpos);            //留下足迹
e = CreateSElem(curstep,curpos,1);    //创建元素
Push(s,e);
if( PosEqual(curpos,end) )    return TRUE;
curpos =NextPos(curpos,1);            //获得下一节点:当前位置的东邻
curstep++;                            //探索下一步
}
else{                        //当前位置不能通过
if(!StackEmpty(s))
{
Pop(s,e);
while(e.di==4 && !StackEmpty(s) )
{
MarkPrint(maze,e.seat);
Pop(s,e);   //留下不能通过的标记,并退回一步
}
if(e.di<4)
{
e.di++; //换一个方向探索
Push(s,e);
curpos = NextPos(e.seat,e.di);    //设定当前位置是该方向上的相邻块
}

}
}
}while(!StackEmpty(s));
return FALSE;
}
main()

#include "AFXSTD.H"
#include "Stack.h"
#include "Maze.h"
int main()
{
char cmd;//命令
int i,j;
PosType start,end;
MazeType maze;
int a[ROW][COL]={
{0,0,1,0,0,0,0,1,0,1},
{1,0,1,0,1,1,0,1,1,0},
{1,0,0,0,1,1,0,0,0,1},
{0,1,0,1,1,0,0,1,1,1},
{0,1,0,0,1,1,0,1,0,0},
{1,0,1,0,1,1,0,0,1,0},
{0,1,1,0,0,0,1,0,1,1},
{0,1,0,1,1,0,0,0,0,0},
{0,1,1,0,0,1,1,1,1,0},
{1,0,1,0,1,1,0,0,1,0}
};
printf("\n----------原始迷宫(不加外围围墙)(0 表示通路,1 表示障碍)---------\n");
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}

InitMaze(maze,a,ROW,COL);
do{
int flag=0;//是不是越界de标志。0即没越界执行下一步,1即循环再次输入
do{
printf("\n输入迷宫入口坐标( <1,1>到<10,10>之间 ): ");
scanf("%d%d",&start.row,&start.col);
if(start.col>maze.n || start.row>maze.m ||start.col<=0||start.row<=0){
printf("越界!");
flag=1;
}
else flag=0;
}while(flag);
do{
printf("输入迷宫出口坐标( <1,1>到<10,10>之间 ): ");
scanf("%d%d",&end.row , &end.col);
if( end.col>maze.n || end.row>maze.m ||end.col<=0||end.row<=0){
printf("越界!\n");
flag=1;
}
else flag=0;
}while(flag);
if(MazePath(maze,start,end))//找到一条路径
{
printf("\n---------从当前入口到出口有通路 * 组成的路径----------\n");
PrintMaze(maze,ROW,COL);
}
else
printf("\n---------从入口到出口没有通路!-----\n");
printf("\n 需要继续吗?: ");
scanf(" %c",&cmd);
}while(cmd=='Y'||cmd=='y');
return 0;
}
所需功能可以实现下面将测试结果显示出来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: