用栈的思想写的动态迷宫求解

来源:互联网 发布:诸葛亮网络用语含义 编辑:程序博客网 时间:2024/04/30 09:28

以下是Stack.h

/** * name:迷宫求解  * auther:gubojun * time:2012-10  */ //---------------------------------------------------#include<stdio.h>#include<stdlib.h> #define seqstacksize  1000  /*栈的最大空间大小*/typedef struct{int ord;//通道块在路径上的“序号”int seat[2];//通道块在迷宫中的“坐标位置”int dir;//从此通道块走到下一通道块的“方向”}mazeType;//栈的元素类型 //---------------基本操作-------------------------------//构造一个空栈s typedef mazeType datatype;typedef struct stack {    datatype  data[seqstacksize];  /*向量data用于存储栈数据*/    int  top;                    /*栈顶指示*/}seqstack;void initstack(seqstack *s)  /*栈初始化*/{ s->top=-1;}int stackempty (seqstack *s) /*判栈空*/{ return s->top==-1;}int stackfull(seqstack *s)   /*判栈满*/{ return s->top==seqstacksize-1;}/*进栈*/void push(seqstack *s, datatype x){ s->data[++s->top]=x;}/*出栈*/datatype pop(seqstack *s){ return s->data[s->top--];}/*取栈顶元*/datatype stacktop(seqstack *s){ return s->data[s->top];}

 

下面是主程序:

/** * name:迷宫求解  * auther:gubojun * time:2012-10  * 本程序算法来自《数据结构》(严蔚敏版)P51 */ //---------------------------------------------------#include"Stack.h"#include <windows.h>#define MAZE_X 15#define MAZE_Y 15int maze[MAZE_X][MAZE_Y];int curstep=1;//实现光标定位到(x,y)void gotoxy(int x, int y){   COORD coord = {x, y};   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}void colorxy(int x, int y){HANDLE hOut;hOut = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hOut, x|y);}//验证当前位置是否可以通过,即是未曾走到过的通道块 //1可以通过,0不能通过 int Pass(int maze[MAZE_X][MAZE_Y],int curpos[2]){if(maze[curpos[0]][curpos[1]]==0) return 1;return 0;}//留下足迹 void FootPrint(int maze[MAZE_X][MAZE_Y],int curpos[2]){maze[curpos[0]][curpos[1]]=curstep;}//下一个点void NextPos(int curp[2],int i){if(i==1)  curp[1]++;else if(i==2) curp[0]--;else if(i==3) curp[1]--;else if(i==4) curp[0]++;}//当前位置不可以通过void MarkPrint(int maze[MAZE_X][MAZE_Y],int pos[2]){maze[pos[0]][pos[1]]=-1;}int MazePath(int maze[MAZE_X][MAZE_Y],int start[2],int end[2]){seqstack *s;mazeType *e;int i;int curpos[2];curpos[0]=start[0];curpos[1]=start[1];curstep=1;s=(seqstack*)malloc(sizeof(seqstack));e=(mazeType*)malloc(sizeof(mazeType));initstack(s);do{if(Pass(maze,curpos)){//当前位置可以通过,即是未曾走到过的通道块 FootPrint(maze,curpos);//留下足迹//----------------打印---------- gotoxy(curpos[1]*2,curpos[0]);colorxy(0x0a,0x0a);printf("+");Sleep(200);//------------------------------ e->ord=curstep;e->seat[0]=curpos[0];e->seat[1]=curpos[1];e->dir=1;push(s,*e);if(curpos[0]==end[0]&&curpos[1]==end[1]){for(i=0;i<s->top;i++){//----------------打印----------gotoxy(s->data[i].seat[1]*2,s->data[i].seat[0]);colorxy(0x0a,0x0c);if(s->data[i].dir==1)printf("%c",26);else if(s->data[i].dir==2)printf("%c",24);else if(s->data[i].dir==3)printf("%c",27);else if(s->data[i].dir==4)printf("%c",25);Sleep(500);//printf("%d,%d\n",s->data[i].seat[0],s->data[i].seat[1]);colorxy(0x0f,0x0f);//------------------------------}return 1;} NextPos(curpos,1);curstep++;}else{if(!stackempty(s)){*e=pop(s);while(e->dir==4&&!stackempty(s)){MarkPrint(maze,e->seat);//----------------打印----------gotoxy(e->seat[1]*2,e->seat[0]);colorxy(0x0a,0x0a);printf(" ");Sleep(200);//------------------------------*e=pop(s);}if(e->dir<4){e->dir++;push(s,*e);curpos[0]=e->seat[0];curpos[1]=e->seat[1];NextPos(curpos,e->dir);}}}}while(!stackempty(s));return 0;}int main(){int maze[MAZE_X][MAZE_Y]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  -1, 0, 0,-1, 0, 0, 0,-1, 0,-1, 0,-1,-1, 0,-1, -1, 0, 0,-1, 0, 0, 0,-1, 0,-1, 0,-1,-1, 0,-1,  -1, 0, 0, 0, 0,-1,-1,-1, 0, 0, 0, 0, 0, 0,-1,  -1, 0,-1,-1,-1, 0,-1, 0, 0,-1, 0,-1,-1, 0,-1, -1, 0, 0, 0,-1, 0, 0,-1, 0,-1, 0,-1, 0, 0,-1,-1, 0,-1, 0,-1, 0,-1,-1, 0,-1,-1,-1, 0, 0,-1,  -1, 0,-1,-1,-1, 0,-1,-1, 0,-1, 0,-1, 0,-1,-1,  -1, 0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0,-1,  -1, 0,-1,-1,-1,-1, 0,-1,-1, 0, 0,-1,-1, 0,-1,-1, 0, 0, 0, 0, 0, 0,-1,-1, 0, 0, 0, 0, 0,-1,-1, 0,-1,-1,-1,-1,-1, 0, 0, 0,-1, 0,-1,-1,-1,-1, 0, 0, 0, 0,-1,-1, 0,-1,-1,-1, 0, 0, 0,-1,-1, 0,-1,-1, 0, 0, 0, 0, 0, 0, 0,-1,-1, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};int start[2]={1,1};int end[2]={13,13};int i,j;for(i=0;i<MAZE_X;i++){for(j=0;j<MAZE_Y;j++){if(maze[i][j]==-1)printf("##");elseprintf("  ");}printf("\n");}//----------------打印起点和终点----------colorxy(0x0b,0x0c);gotoxy(start[1]*2,start[0]);printf("@");gotoxy(end[1]*2,end[0]);printf("@");//---------------------------------------- i=MazePath(maze,start,end);if(i==0){gotoxy(0,MAZE_Y+1);printf("没有找到出口!\n");}else{gotoxy(0,MAZE_Y+1);printf("找到了出口!\n");}}



 

原创粉丝点击