数据结构迷宫问题源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

数据结构迷宫问题源代码
#include<stdio.h> #include<malloc.h>
#include<stdlib.h>
#include <conio.h>
#include"typedef.h"
#include "ADTStack.h"
#include"maze.h"
#include"CONSTANT.h"
#define MAXLEN 15
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define NULL 0
PosType start;
PosType end;
MazeType maze;
bool found;
#define MAXLEN 15//迷宫包括外墙最大行列数目#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
// 坐标位置类型
typedef struct
{
int r,c;
}PosType;//迷宫中r行c列的位置
//迷宫类型
typedef struct
{
int step;
PosType seat; //当前的坐标位置
int di; //往下一坐标位置的方向
}ElemType;
//结点类型
typedef struct
{ ElemType *base;
ElemType *top;
int size;
}Stack;
Status FootPrint(MazeType maze,PosType curpos);
Status MarkPrint(MazeType maze,PosType curpos);
void PrintMaze(MazeType maze);
Status MazePath(MazeType maze,PosType start,PosType end);
void InitMaze(MazeType maze, char a[MAXLEN][MAXLEN], int row, int col);
Status Pass(MazeType maze, PosType curpos);
PosType NextPos(PosType curpos,int i);
typedef struct NodeType
{
ElemType data;
NodeType *next;
}NodeType,*LinkType;
//栈类型
typedef struct
{
int r;
int c;
char arr[MAXLEN][MAXLEN];//可取' ','*','@','#' }MazeType;
Status InitStack(Stack &S);
//InitStack
Status Push(Stack &S,ElemType e) ;
Status Pop(Stack &S, ElemType &e) ;
Status DestroyStack(Stack &S);
//DestroyStack
void ClearStack(Stack &S);
//ClearStack
Status StackEmpty(Stack S);
//StackEmpty
int StackLength(Stack S);
void StackTraverse(Stack S, Status(*visit)(ElemType e));
//创建栈
void Initialization()
{
printf("\n*********************************************************"); printf("\n * CreatMaze--c MazePath--m PrintMaze Quit*"); printf("\n*****************************************************"); printf("\n * Operation: *");
printf("\n************************************************");
printf("\n\n enter a operation code:c,m,p,q:");
}
//读入操作命令符,显示提示信息
void ReadCommand(char &cmd)
{
do
{cmd=getche();
}while(!(cmd=='c'||cmd=='m'||cmd=='p'));
}
//解释ch--具体执行
void Interpret(char cmd)
{
switch(cmd)
{
case 'c':{
int rnum, cnum, i=0,m=1,n=1;
char a2[MAXLEN][MAXLEN];
char input[20];
char data[1000];
printf("\n请输入迷宫数据文件名!\n");
scanf("%s",input);
FILE *fp;
fp=fopen(input,"r");
if(!fp)
{
printf("\n不能打开文件\n");
break;
while(!feof(fp))
{
fscanf(fp,"%s",&data[i]);
if(i==0)
{
rnum=(int
)data[i]-(int)'0';
}
if(i==1)
{
cnum=(int)data[i]-(int)'0';
}
if(i>=2)
{
if(n>cnum){m++;n=1;}
a2[m][n]=data[i];
n++;
}
i++;
}
fclose(fp);
InitMaze(maze, a2, rnum, cnum);
printf("\n迷宫建立完成!!\n");
break;
}
case 'm':{
printf("\n请输入迷宫入口的坐标,以空格为间隔:--"); scanf("%d %d",&start.r,&start.c);
printf("\n请输入迷宫出口的坐标,以空格为间隔:--"); scanf("%d %d",&end.r,&end.c);
MazePath(maze, start, end);
break;
}
case 'p':{
if(found)
{
printf("\n求解迷宫的结果如下--\n");
PrintMaze(maze);
}
else printf("\n找不到路径!\n");
}
}
void main()
{
char cmd;
printf(" welcome to the game!!! "); Initialization();
do
{
ReadCommand(cmd); //读入一个操作符命令
Interpret(cmd); //解释执行命令操作符
}while(cmd!='q');
}
#include "stdio.h"
#include "malloc.h"
#include"typedef.h"
#include"CONSTANT.h"
Status InitStack(Stack &S)
{ // 构造一个空栈S
S.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if (!S.base) return OVERFLOW; //存储分配失败
S.top = S.base;
S.size = STACK_INIT_SIZE;
return OK;
} //InitStack
Status Push(Stack &S,ElemType e)
{
if (S.top - S.base >= S.size)
{//栈满,追加存储空间
S.base = (ElemType*)realloc( S.base,(S.size + STACKINCREMENT)*sizeof (ElemType)); if (!S.base) return OVERFLOW; //存储分配失败
S.top = S.base + S.size;
S.size += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}//Push
Status Pop(Stack &S, ElemType &e)
{
// 若栈不空,则删除S的栈顶元素,
// 用e返回其值,并返回OK;
// 否则返回ERROR
if (S.top == S.base) return ERROR;
e = *--S.top;
return OK;
}//Pop
Status DestroyStack(Stack &S)
{ //释放栈S所占据的存储空间
if(!S.base) return ERROR;
free(S.base);S.base=NULL;
S.top= NULL;
return OK;
}//DestroyStack
Status ClearStack(Stack &S)
{ //将栈S清为空栈????
S.top=S.base;
return OK;
}//ClearStack
Status StackEmpty(Stack S)
{
//如果栈为空栈,则返回TRUE,否则返回FALSE
if(S.top==S.base)return TRUE;
else return FALSE;
}//StackEmpty
int StackLength(Stack S)
{ //返回栈S的长度,实际上是栈中元素的个数
return S.top-S.base;
}//StackLength
Status StackTraverse(Stack S,Status (*visit)(ElemType e))
{ int i;
//从栈底到栈顶依次对栈中每个元素调用visit函数,如果visit失败,则操作失败if(S.top==S.base) return ERROR;
for(i=0;i<S.top - S.base;i++)
if(visit(S.base[i])==ERROR) return ERROR;
return OK;
} //StackTraverse
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include"typedef.h"
#include"CONSTANT.h"
#include "ADTStack.h"
Status MarkPrint(MazeType maze,PosType curpos)
{
maze.arr[curpos.r][curpos.c]='@';//"@"表示曾走过但不通return OK;
}
//曾走过而且是通路标记并返回OK
Status FootPrint(MazeType maze,PosType curpos)
{
maze.arr[curpos.r][curpos.c]='*';//"*"表示可通
return OK;
}
PosType NextPos(PosType &curpos,int i)
{
PosType cpos;
cpos=curpos;
scanf("%d",&i);
switch(i)
{ //1.2.3.4分别表示东,南,西,北方向
case 1 : cpos.c+=1;
break;
case 2 : cpos.r+=1;
break;
case 3 : cpos.c-=1;
break;
case 4 : cpos.r-=1;
break;
}
return cpos;
}
//判断当前位置是否可通
Status Pass(MazeType maze, PosType curpos)
{
if(maze.arr[curpos.r][curpos.c]==' ')
return TRUE;
else
return FALSE;
}
//创建迷宫
//按照用户输入的二维数组(或),设置迷宫maze的初值,包括加上边缘一圈的值void InitMaze(MazeType maze, char a[MAXLEN][MAXLEN], int row, int col)
{
maze.r=row;
maze.c=col;
int i;
for( i=0;i<=col+1;i++)
{
a[0][i]='1';
a[row+1][i]='1';
}
for(i=0;i<=row+1;i++)
{
a[i][0]='1';
a[i][col+1]='1';
}
for(i=0;i<=maze.r+2;i++)
{
for(int j=0;j<maze.c+2;j++)
{
if(a[i][j]=='1')
maze.arr[i][j]='#';
else
maze.arr[i][j]=' ';
}
}
}
Status MazePath(MazeType maze,PosType start,PosType end)
{
//求解迷宫maze中,从入口start到出口end的一条路径,若存在,返回TRUE,否则返回FALSE PosType curpos;
int curstep=1;
Stack S;
bool found;
ElemType e;
InitStack(S);
curpos=start;//设定"当前位置"为"入口位置"
found=FALSE;
do
{
if(Pass(maze,curpos))
{
//当前位置可以通过,即是未曾走到过的通道块留下足迹
FootPrint(maze,curpos);//做可以通过的标识
e.seat=curpos;
e.di=1; //为栈顶元素赋值
Push(S,e); //加入路径
if(curpos.r==end.r && curpos.c==end.c) found=TRUE;//如果到达终点返回true
else
{
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);
curstep--;
}
if(e.di<4)
{
e.di++; //换下个方向
Push(S,e); //
curpos=NextPos(e.seat,e.di); //进行探索
}
}
}
while(!StackEmpty(S)&&!found);
//DestroyStack(S);
return found;
}
//MazePath将标记路径
信息的迷宫(字符型方阵)输出到终端(包括外墙) void PrintMaze(MazeType maze)
{
for(int i=0;i<=maze.r+2;i++)
{
for(int j=0;j<=maze.c+2;j++)
{
printf(" %c",maze.arr[i][j]);//输出迷宫
}
printf("\n");
}
}。

相关文档
最新文档