数据结构迷宫问题的C 代码
数据结构迷宫问题源代码
![数据结构迷宫问题源代码](https://img.taocdn.com/s3/m/022489253968011ca3009128.png)
数据结构迷宫问题源代码#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 0PosType 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 10typedef 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);//InitStackStatus Push(Stack &S,ElemType e) ;Status Pop(Stack &S, ElemType &e) ;Status DestroyStack(Stack &S);//DestroyStackvoid ClearStack(Stack &S);//ClearStackStatus StackEmpty(Stack S);//StackEmptyint 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){ // 构造一个空栈SS.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));if (!S.base) return OVERFLOW; //存储分配失败S.top = S.base;S.size = STACK_INIT_SIZE;return OK;} //InitStackStatus 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;}//PushStatus Pop(Stack &S, ElemType &e){// 若栈不空,则删除S的栈顶元素,// 用e返回其值,并返回OK;// 否则返回ERRORif (S.top == S.base) return ERROR;e = *--S.top;return OK;}//PopStatus DestroyStack(Stack &S){ //释放栈S所占据的存储空间if(!S.base) return ERROR;free(S.base);S.base=NULL;S.top= NULL;return OK;}//DestroyStackStatus ClearStack(Stack &S){ //将栈S清为空栈????S.top=S.base;return OK;}//ClearStackStatus StackEmpty(Stack S){//如果栈为空栈,则返回TRUE,否则返回FALSEif(S.top==S.base)return TRUE;else return FALSE;}//StackEmptyint StackLength(Stack S){ //返回栈S的长度,实际上是栈中元素的个数return S.top-S.base;}//StackLengthStatus 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;}//曾走过而且是通路标记并返回OKStatus 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;elsereturn 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]='#';elsemaze.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;//如果到达终点返回trueelse{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");}}。
迷宫问题栈算法c++代码
![迷宫问题栈算法c++代码](https://img.taocdn.com/s3/m/fff3740c86c24028915f804d2b160b4e767f81fc.png)
迷宫问题栈算法c++代码迷宫问题是一个经典的寻路问题,目标是从迷宫的入口找到出口的路径。
栈是一个常用的数据结构,可以用来实现迷宫问题的解决方法。
下面是使用栈算法解决迷宫问题的C++代码。
```cpp#include <iostream>#include <stack>#include <vector>using namespace std;// 定义迷宫的大小const int N = 5;const int M = 5;// 定义迷宫的地图,0 表示可通行的路径,1 表示墙壁int maze[N][M] = {{0, 1, 0, 0, 0},{0, 1, 0, 1, 0},{0, 0, 0, 0, 0},{0, 1, 1, 1, 0},{0, 0, 0, 1, 0}};// 定义方向数组,分别表示右、下、左、上四个方向移动int dx[] = {1, 0, -1, 0};int dy[] = {0, 1, 0, -1};// 定义迷宫中的点struct Point {int x, y;};// 判断某个点是否在迷宫内且可通行bool isValid(int x, int y) {return x >= 0 && x < N && y >= 0 && y < M && maze[x][y] == 0;}// 使用栈算法寻找迷宫路径vector<Point> findPath(int startX, int startY, int endX, int endY) {stack<Point> s;vector<Point> path;bool visited[N][M] = {false};// 将起点加入栈中s.push({startX, startY});visited[startX][startY] = true;while (!s.empty()) {// 获取栈顶点Point current = s.top();s.pop();// 判断是否到达终点if (current.x == endX && current.y == endY) { path.push_back(current);break;}// 遍历四个方向for (int i = 0; i < 4; i++) {int nextX = current.x + dx[i];int nextY = current.y + dy[i];// 判断下一个点是否合法且未访问过 if (isValid(nextX, nextY)&& !visited[nextX][nextY]) {s.push({nextX, nextY});visited[nextX][nextY] = true; }}// 如果栈为空,说明无法到达终点if (s.empty()) {cout << '无法找到路径!' << endl; return path;}}// 反向输出路径while (!s.empty()) {path.push_back(s.top());s.pop();}return path;}int main() {int startX, startY, endX, endY;cout << '请输入起点坐标(以空格分隔):';cin >> startX >> startY;cout << '请输入终点坐标(以空格分隔):';cin >> endX >> endY;vector<Point> path = findPath(startX, startY, endX, endY);if (!path.empty()) {cout << '找到路径!路径长度为:' << path.size() << endl; cout << '路径为:';for (int i = path.size() - 1; i >= 0; i--) {cout << '(' << path[i].x << ', ' << path[i].y << ')';if (i != 0) {cout << ' -> ';}}}return 0;}```以上代码通过栈算法实现了迷宫问题的解决方法。
c语言~走迷宫
![c语言~走迷宫](https://img.taocdn.com/s3/m/3f50910576c66137ee0619a8.png)
本程序代码为C语言解决数据结构(严蔚敏)中关于迷宫的问题。
程序不仅实现迷宫路径查找,还实现文字描述路径功能可以直接粘贴到vc6.0中运行【代码如下】# include <stdio.h> # include <malloc.h> # define null 0typedef struct{int (*base)[2];int (*top)[2];int listlen;}sqlist;int topelem[2]; //栈顶元素void creatstack(sqlist *mazepath); //创建一个存储路径的栈void creatmap(int (*mazemap)[10]); //创建迷宫图纸void printmap(int (*mazemap)[10]);void footprint(int x,int y,int k,int (*mazemap)[10]);int position(int x,int y); //判断是否到终点int passroad(int x,int y,int (*mazemap)[10]);void findpath(int (*mazemap)[10],sqlist *mazepath); //在mazemap当中寻找mazepahtvoid printpath(sqlist *mazepath);void roadinwords(sqlist *mazepath); //文字叙述如何走迷宫void push(int x,int y,sqlist *mazepath); //栈操作void pop(sqlist *mazepath);void gettop(sqlist *mazepath);void main(){sqlist mazepath;creatstack(&mazepath); //创建一个存储路径的栈int mazemap[10][10]={1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,1,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0,1,0,0,0,1,0,0,1,1,0,1,1,1,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1};// creatmap(mazemap); //创建迷宫图纸printf("迷宫原图为:\n");printmap(mazemap);findpath(mazemap,&mazepath); //在mazemap当中寻找mazepaht printf("走出迷宫图纸为:\n");printmap(mazemap);printf("走出迷宫文字叙述为:\n");roadinwords(&mazepath);// printpath(&mazepath);}void findpath(int (*mazemap)[10],sqlist *mazepath){int x,y,flag=0,k=0,next; //位置是否可通,flag=0通,1墙,2通但不可走x=1;y=1; //获取初始位置push(x,y,mazepath); //起点位置进栈footprint(x,y,6,mazemap);while(flag==0 && k!=162) //flag==1到达终点,0未到达终点{if(passroad(x,y+1,mazemap)==0)push(x,y+1,mazepath),y=y+1,footprint(x,y,6,mazemap);else if(passroad(x+1,y,mazemap)==0)push(x+1,y,mazepath),x=x+1,footprint(x,y,6,mazemap);else if(passroad(x,y-1,mazemap)==0)push(x,y-1,mazepath),y=y-1,footprint(x,y,6,mazemap);else if(passroad(x-1,y,mazemap)==0)push(x-1,y,mazepath),x=x-1,footprint(x,y,6,mazemap);elsefootprint(x,y,2,mazemap),pop(mazepath),gettop(mazepath),x=topelem[0],y= topelem[1];// printmap(mazemap);k++;flag=position(x,y); //判断是否到达终点// printf("flag==%d\n",flag);}}void creatstack(sqlist *mazepath){mazepath->base=(int (*)[2])malloc(120*sizeof(int (*)[2]));mazepath->top=mazepath->base;mazepath->listlen=120;}void push(int x,int y,sqlist *mazepath){**(mazepath->top)=x;*(*(mazepath->top)+1)=y;mazepath->top++;}void pop(sqlist *mazepath){if(mazepath->top!=mazepath->base)mazepath->top--;}void printmap(int (*mazemap)[10]){int (*p)[10];p=mazemap;int i,j;printf(" \n\n\n");for(i=0;i<10;i++){for(j=0;j<10;j++){if(j==0)printf(" ");if(*(*(p+i)+j)==0)printf("▇");else if(*(*(p+i)+j)==1)printf("□");else if(*(*(p+i)+j)==6)printf("★");elseprintf("▇");if(j==9)printf("\n");}}printf("\n\n");}void printpath(sqlist *mazepath){int (*p)[2];p=mazepath->base;while(p!=mazepath->top){printf("x=%d,y=%d\n",**p,*(*p+1));p++;}}void gettop(sqlist *mazepath){int (*p)[2];int (*q)[2];p=mazepath->base;while(p!=mazepath->top){q=p;p++;}topelem[0]=**q;topelem[1]=*(*q+1);}void footprint(int x,int y,int k,int (*mazemap)[10]){if(x<10 && y<10)*(*(mazemap+x)+y)=k;}int position(int x,int y){int flag;if(x==8 && y==8)flag=1;elseflag=0;return(flag);}int passroad(int x,int y,int (*mazemap)[10]) {int num=1;if(x<10 && y<10)num=*(*(mazemap+x)+y);return(num);}void roadinwords(sqlist *mazepath){int x=1,y=1,i=0;int (*p)[2];p=mazepath->base;p++;while(p!=mazepath->top){if(x==**p && y+1==*(*p+1))printf("向右走→→"),x=**p,y=*(*p+1);else if(x+1==**p && y==*(*p+1))printf("向下走→→"),x=**p,y=*(*p+1);else if(x==**p && y-1==*(*p+1))printf("向左走→→"),x=**p,y=*(*p+1);else if(x-1==**p && y==*(*p+1))printf("向上走→→"),x=**p,y=*(*p+1);i++;if(i%3==0)printf("\n");p++;}printf("\n");}。
数据结构迷宫问题的C++代码
![数据结构迷宫问题的C++代码](https://img.taocdn.com/s3/m/a820f50cde80d4d8d15a4f7b.png)
数据结构课程设计——迷宫问题求解代码问题描述及要求:迷宫问题求解输入:第一行n,m表示迷宫大小n*m之后有n行m列,全由01组成,0表示路,1表示墙入口设为(0, 0)出口设为(n-1, m-1)输出:输出从入口到出口的所有不同解,每组解的第一行打印一个数表示第几组解,之后k行表示路径,如:1(0, 0)(0, 1)(1, 1)代码部分(已测试,可直接运行):#include <cstdio>#include <cstring>#define N 255int n,m,cnt;int maze[N][N],step[N][N];struct Point{int x,y;}path[N*N];int oper[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};bool isvaild(int x,int y){if (x >= 0 && x < n && y >= 0 && y < m && !maze[x][y] && !step[x][y])return true;return false;}void dfs(int x,int y,int len){if (x == n-1 && y == m-1){cnt++;printf("%d\n",cnt);for (int i = 0;i < len;i++)printf("(%d,%d)\n",path[i].x,path[i].y);return;}for (int i = 0;i < 4;i++)if (isvaild(x + oper[i][0],y + oper[i][1])){Point tmp;tmp.x = x + oper[i][0];tmp.y = y + oper[i][1];path[len] = tmp;step[tmp.x][tmp.y] = 1;dfs(tmp.x, tmp.y, len+1);step[tmp.x][tmp.y] = 0;}}void work(){step[0][0] = 1;Point cur;cur.x = 0;cur.y = 0;path[0] = cur;dfs(0, 0, 1);}int main(){scanf("%d%d", &n, &m);for (int i = 0;i < n;i++)for (int j = 0;j < m;j++)scanf("%d", &maze[i][j]);cnt = 0;memset(step, 0, sizeof(step));work();return 0;。
栈的应用-迷宫问题-数据结构(C语言版)-源代码(直接运行)
![栈的应用-迷宫问题-数据结构(C语言版)-源代码(直接运行)](https://img.taocdn.com/s3/m/720ef8f0941ea76e58fa04fa.png)
#include <stdio.h>#include <stdlib.h>#include <string.h>#define STACK_INIT_SIZE 100#define INCREMENT 10typedef struct{int r;int c;}zuobiao;typedef struct{int ord; //在当前坐标上的“标号”zuobiao seat; //坐标int di; //走向下一通道的方向}lujing;typedef struct{int sz[10][10];}Maze;typedef struct SqStack{lujing *base;lujing *top;int size;}SqStack;int initStack(SqStack *s){s->base = (lujing *)malloc(STACK_INIT_SIZE*sizeof(lujing) );if(!s->base)return -1;s->top = s->base;s->size = STACK_INIT_SIZE;return 0;}int push(SqStack *s, lujing e){if(s->top - s->base >= s->size){s->base = (lujing *)realloc(s->base, (s->size+INCREMENT)*sizeof(lujing));if(!s->base)return -1;s->top = s->base+s->size;s->size += INCREMENT;}*s->top++ = e;return 0;}int pop(SqStack *s,lujing *e){if(s->top == s->base)return -1;*e = *(--s->top);return 0;}int isEmpty(SqStack *s){if(s->base == s->top)return 1;elsereturn 0;}int pass( Maze maze,zuobiao dqzb) {if (maze.sz[dqzb.r][dqzb.c]==1)return 1; // 如果当前位置是可以通过,返回1else return 0; // 否则返回0}void footPrint(Maze &maze,zuobiao dqzb) {maze.sz[dqzb.r][dqzb.c]=9;}void markPrint(Maze &maze,zuobiao dqzb) {maze.sz[dqzb.r][dqzb.c]=4;}zuobiao nextPos(zuobiao dqzb, int Dir) {zuobiao xzb;switch (Dir) {case 1:xzb.r=dqzb.r;xzb.c=dqzb.c+1;break;case 2:xzb.r=dqzb.r+1;xzb.c=dqzb.c;break;case 3:xzb.r=dqzb.r;xzb.c=dqzb.c-1;break;case 4:xzb.r=dqzb.r-1;xzb.c=dqzb.c;break;}return xzb;}int mazePath(Maze &maze, zuobiao start, zuobiao end){SqStack *s = (SqStack *)malloc(sizeof(SqStack));initStack(s);zuobiao dqzb = start; // 设定"当前位置"为"入口位置"lujing e;int curstep = 1; // 探索第一步do {if (pass(maze,dqzb)) { // 当前位置可通过,即是未曾走到过的通道块footPrint(maze,dqzb); // 留下足迹e.di =1;e.ord = curstep;e.seat= dqzb;push(s,e); // 加入路径if (dqzb.r == end.r && dqzb.c==end.c)return 0; // 到达终点(出口)dqzb = nextPos(dqzb, 1); // 下一位置是当前位置的东邻curstep++; // 探索下一步} else { // 当前位置不能通过if (!isEmpty(s)) {pop(s,&e);while (e.di==4 && !isEmpty(s)) {markPrint(maze,e.seat);pop(s,&e); // 留下不能通过的标记,并退回一步}if (e.di<4) {e.di++; // 换下一个方向探索push(s, e);dqzb = nextPos(e.seat, e.di); // 当前位置设为新方向的相邻块}}}} while (!isEmpty(s) );return -1;}void main(){printf(" *----Δ迷宫求解Δ----*\n\n");printf("---迷宫如下所示:(说明:0表示墙;1表示可以通过)");printf("\n");Maze maze; //声明mazemaze.sz[0][0]= 0;maze.sz[0][1]= 0;maze.sz[0][2]= 0;maze.sz[0][3]= 0;maze.sz[0][4]= 0;maze.sz[0][5]= 0;maze.sz[0][6]= 0;maze.sz[0][7]= 0;maze.sz[0][8]= 0;maze.sz[0][9]= 0;maze.sz[1][0]= 0;maze.sz[1][1]= 1;maze.sz[1][2]= 1;maze.sz[1][3]= 0;maze.sz[1][4]= 1;maze.sz[1][5]= 1;maze.sz[1][6]= 1;maze.sz[1][7]= 0;maze.sz[1][8]= 1;maze.sz[1][9]= 0;maze.sz[2][0]= 0;maze.sz[2][1]= 1;maze.sz[2][2]= 1;maze.sz[2][3]= 0;maze.sz[2][4]= 1;maze.sz[2][5]= 1;maze.sz[2][6]= 1;maze.sz[2][7]= 0;maze.sz[2][8]= 1;maze.sz[2][9]= 0;maze.sz[3][0]= 0;maze.sz[3][1]= 1;maze.sz[3][2]= 1;maze.sz[3][3]= 1;maze.sz[3][4]= 1;maze.sz[3][5]= 0;maze.sz[3][6]= 0;maze.sz[3][7]= 1;maze.sz[3][8]= 1;maze.sz[3][9]= 0;maze.sz[4][0]= 0;maze.sz[4][1]= 1;maze.sz[4][2]= 0;maze.sz[4][3]= 0;maze.sz[4][4]= 0;maze.sz[4][5]= 1;maze.sz[4][6]= 1;maze.sz[4][7]= 1;maze.sz[4][8]= 1;maze.sz[4][9]= 0;maze.sz[5][0]= 0;maze.sz[5][1]= 1;maze.sz[5][2]= 1;maze.sz[5][3]= 1;maze.sz[5][4]= 0;maze.sz[5][5]= 1;maze.sz[5][6]= 1;maze.sz[5][7]= 0;maze.sz[5][8]= 1;maze.sz[5][9]= 0;maze.sz[6][0]= 0;maze.sz[6][1]= 1;maze.sz[6][2]= 0;maze.sz[6][3]= 1;maze.sz[6][4]= 1;maze.sz[6][5]= 1;maze.sz[6][6]= 0;maze.sz[6][7]= 1;maze.sz[6][8]= 1;maze.sz[6][9]= 0;maze.sz[7][0]= 0;maze.sz[7][1]= 1;maze.sz[7][2]= 0;maze.sz[7][3]= 0;maze.sz[7][4]= 0;maze.sz[7][5]= 1;maze.sz[7][6]= 0;maze.sz[7][7]= 0;maze.sz[7][8]= 1;maze.sz[7][9]= 0;maze.sz[8][0]= 0;maze.sz[8][1]= 0;maze.sz[8][2]= 1;maze.sz[8][3]= 1;maze.sz[8][4]= 1;maze.sz[8][5]= 1;maze.sz[8][6]= 1;maze.sz[8][7]= 1;maze.sz[8][8]= 1;maze.sz[8][9]= 0;maze.sz[9][0]= 0;maze.sz[9][1]= 0;maze.sz[9][2]= 0;maze.sz[9][3]= 0;maze.sz[9][4]= 0;maze.sz[9][5]= 0;maze.sz[9][6]= 0;maze.sz[9][7]= 0;maze.sz[9][8]= 0;maze.sz[9][9]= 0;int i,j,sum;for (i = 0; i < 10; i++) {for (j = 0; j < 10; j++){printf("%4d",maze.sz[i][j]);sum++;}if(sum%10==0)printf("\n");}printf("\n");printf("---请输入1求解路径图:");int a;scanf("%d",&a);if(a==1){zuobiao rukou,chukou;rukou.r=1;rukou.c=1;chukou.r=8;chukou.c=8;mazePath(maze,rukou,chukou);printf("\n");printf("---图解如下所示:(说明:0表示墙;1表示可以通过;4表示走不通;9表示所走的路径)\n");for (i = 0; i < 10; i++) {for (j = 0; j < 10; j++){printf("%4d",maze.sz[i][j]);sum++;}if(sum%10==0)printf("\n");}}}。
数据结构迷宫问题源代码
![数据结构迷宫问题源代码](https://img.taocdn.com/s3/m/c66725f2aef8941ea76e0569.png)
#include<stdio.h>#include<stdlib.h>#include"Status.h"typedef struct{int x;int y;}posType;typedef struct{posType pos;int dirc;}SElemType;#include"SqStack.h"#define Row 12#define Col 12posType nextPos(posType curPos,int d){posType pos;switch(d){case 1: pos.x=curPos.x;pos.y=curPos.y+1;break;case 2: pos.x=curPos.x+1;pos.y=curPos.y;break;case 3: pos.x=curPos.x;pos.y=curPos.y-1;break;case 4: pos.x=curPos.x-1;pos.y=curPos.y;break;}return pos;}Status canGo(posType pos,int M[Row][Col]){if(M[pos.x][pos.y]==0) return TRUE;else return FALSE;}Status Maze(int M[Row][Col],posType start,posType end){ SqStack S;SElemType e;posType curPos;int d,step;InitSqStack(&S,100);curPos=start;d=4;do{if(canGo(curPos,M)){M[curPos.x][curPos.y]=2;e.pos=curPos;e.dirc=d;Push(&S,e);if(curPos.x==end.x&&curPos.y==end.y) break;d=1;curPos=nextPos(curPos,d);}else if(d<4){d++;getTop(S,&e);curPos=nextPos(e.pos,d);}else if(!stackIsEmpty(S)){Pop(&S,&e);d=e.dirc;curPos=e.pos;}}while(!stackIsEmpty(S));if(!stackIsEmpty(S)){Pop(&S,&e);M[e.pos.x][e.pos.y]='e';d=e.dirc;for(step=stackLength(S);step>0;step--){Pop(&S,&e);switch(d){case 1: M[e.pos.x][e.pos.y]=26;break;case 2: M[e.pos.x][e.pos.y]=25;break;case 3: M[e.pos.x][e.pos.y]=27;break;case 4: M[e.pos.x][e.pos.y]=24;break;}d=e.dirc;}M[start.x][start.y]='s';return TRUE;}else return FALSE;}void printMaze(int M[Row][Col],posType start,posType end){int i,j;printf("迷宫:入口(%d,%d),出口(%d,%d)\n",start.x,start.y,end.x,end.y); printf("\t%3c",' ');for(i=0;i<Col;i++)printf("%2d ",i);printf("\n");for(i=0;i<Row;i++){printf("\t%2d",i);for(j=0;j<Col;j++){if(M[i][j]==1)printf("|||");else if(M[i][j]==0)printf(" ");else if(M[i][j]==2)printf(" = ");else printf(" %c",M[i][j]);}printf("\n");}printf("\n");}int main(){int M[Row][Col]={{1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,1,1,0,1,1,1,0,1},{1,0,1,0,0,1,0,0,1,0,0,1},{1,0,1,1,0,0,0,1,1,0,1,1},{1,0,0,1,0,1,0,0,0,0,0,1},{1,1,0,1,0,1,1,0,1,0,1,1},{1,0,0,1,0,0,0,0,1,1,1,1},{1,0,1,0,1,1,1,1,0,0,1,1},{1,0,0,0,0,1,1,0,0,0,1,1},{1,0,1,0,1,0,0,0,1,0,0,1},{1,0,0,0,0,0,1,0,0,1,0,1},{1,1,1,1,1,1,1,1,1,1,1,1} };posType start,end;start.x=1;start.y=1;end.x=10;end.y=10;printMaze(M,start,end);if(Maze(M,start,end)){printf("找到可行路径:\n");printMaze(M,start,end);}else printf("无可行路径!\n");system("pause");return 0;}。
迷宫(direction)C语言代码
![迷宫(direction)C语言代码](https://img.taocdn.com/s3/m/fceee25bcf84b9d529ea7a03.png)
voidmazePath(intmaze[][N],intdirection[][2],intx1,inty1,intx2,inty2) {
inti, j, k, g, h;
PSeqStackst;
DataTypeelement;
1,1,1,1,1,1,1,1,1,1,1
};
mazePath(maze,direction,1,1,6,9);
getchar();
return 0;
}
#include<stdio.h>
#include<conio.h>
intmigong[10][10]= //设置迷宫,最外围1为墙 里边0为可走路径 1为障碍
find=1;
}
if(find==1){ //判断是否找得到
lj[top].d=d;
top++;
lj[top].x=x;
lj[top].y=y;
d=-1;find=0; //重新调整方向
migong[x][y]=-1;}
else{
migong[lj[top].x][lj[top].y]=0;
top--;d=lj[top].d; //找不到的话退栈
case1:x=lj[top].x; y=lj[top].y+1;break;//方向为右
case2:x=lj[top].x+1; y=lj[top].y; break;//方向为下
case3:x=lj[top].x; y=lj[top].y-1;}//方向为左
if(migong[x][y]==0)
数据结构C语言版 非循环顺序队列求解迷宫问题
![数据结构C语言版 非循环顺序队列求解迷宫问题](https://img.taocdn.com/s3/m/a8446848168884868762d68a.png)
数据结构C语言版非循环顺序队列求解迷宫问题利用非循环顺序队列采用广度搜索法求解迷宫问题(一条路径)编译环境:Dev-C++ 4.9.9.2日期:2011年2月12日*/#include <stdio.h>#include <malloc.h>#define M 5 // 迷宫行数(包括外墙)#define N 5 // 迷宫列数(包括外墙)#define D 4 // 移动方向数,只能取4和8。
(8个,可斜行;4个,只可直走)// 移动数组,移动方向由正东起顺时针转struct{int x,y;#if D==8}move[D]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; #endif#if D==4}move[D]={{0,1},{1,0},{0,-1},{-1,0}};#endif// 定义栈元素类型和队列元素类型,两者为相同类型。
typedef struct{int x,y; // 当前点的行值,列值int pre; // 前一点在队列中的序号} SElemType, QElemType;#define STACK_INIT_SIZE 10 // 存储空间初始分配量#define STACKINCREMENT 2 // 存储空间分配增量// 栈的顺序存储表示 P46typedef struct SqStack{SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL SElemType *top; // 栈顶指针int stacksize; // 当前已分配的存储空间,以元素为单位}SqStack; // 顺序栈// 顺序队列(非循环,因为是非循环的,所以需要判断是否溢出#define MAXQSIZE 5 // 最大队列长度(对于循环队列,最大队列长度要减1) typedef struct{QElemType *base;// 初始化的动态分配存储空间相当于一个数组// 头指针,若队列不空,指向队列头元素,相当于一个数组下标int front;// 尾指针,若队列不空,指向队列尾元素的下一个位置相当于一个数组下标int rear;}SqQueue;// 构造一个空队列Qint InitQueue(SqQueue *Q){//分配定长的空间,相当于一个数组(*Q).base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));if(!(*Q).base) // 存储分配失败exit(0);(*Q).front=(*Q).rear=0; //初始化下标return 1;}// 销毁队列Q,Q不再存在int DestroyQueue(SqQueue *Q){if((*Q).base)free((*Q).base);(*Q).base=NULL;(*Q).front=(*Q).rear=0;return 1;}// 若队列Q为空队列,则返回1,否则返回0int QueueEmpty(SqQueue Q){if(Q.front==Q.rear) // 队列空的标志return 1;else}// 插入元素e为Q的新的队尾元素int EnQueue(SqQueue *Q,QElemType e){if((*Q).rear>=MAXQSIZE){ // 队列满,增加1个存储单元(*Q).base=(QElemType *)realloc((*Q).base,((*Q).rear+1)*sizeof(QElemType));if(!(*Q).base) // 增加单元失败return 0;}*((*Q).base+(*Q).rear)=e;(*Q).rear++;return 1;}// 若队列不空,则删除Q的队头元素,用e返回其值,并返回1,否则返回0int DeQueue(SqQueue *Q,QElemType *e){if((*Q).front==(*Q).rear) // 队列空return 0;*e=(*Q).base[(*Q).front];(*Q).front=(*Q).front+1;return 1;}// 构造一个空栈S。
迷宫求解完整代码
![迷宫求解完整代码](https://img.taocdn.com/s3/m/2d0277ce9a89680203d8ce2f0066f5335a816795.png)
迷宫求解完整代码3.本程序包括三个模块1) 主程序模块:void main(){初始化;do{接受命令;处理命令;}while(命令!=”退出”);}2) 栈模块------实现栈的抽象数据类型 3)迷宫模块----实现迷宫抽象数据类型各个模块之间的调用关系如下:主程序模块->迷宫模块 ->栈模块三( 详细设计1. 坐标位置类型typedef struct PosType //定义坐标 {int x; //当前横坐标int y; //当前纵坐标 }PosType;2.迷宫类型int Maze[][];//迷宫存放在一个数字型数组中int MazePath(int maze[N][N],PosType start,PosType end,LinkStack&stack,int n1,int n2)//求解迷宫maze中,从入口Start到出口end的一条路径{stack=NULL;SElemType e;PosType curpos;InitStack(stack);//初始化stackcurpos.x=start.x; //设定当前位置为入口位置curpos.y=start.y;int curstep=1; //探索第一步do{if(Pass(curpos,maze,n1,n2)==1)//当前位置可通{FootPrint(curpos,maze); //留下足迹e.ord=curstep;e.seat.x=curpos.x;e.seat.y=curpos.y;e.di=1;Push(stack,e); //加入路径,入栈if(curpos.x==end.x&&curpos.y==end.y) //到达出口位置return 1;elseNextPos(curpos,1); //探索下一步curstep++;}//ifelse //当前位置不能通过{if(!StackEmpty(stack)){Pop(stack,e);while(e.di==4&&!StackEmpty(stack)){MarkPrint(e.seat,maze);//留下不能通过的标记,并退回一步Pop(stack,e);}//while}if(e.di<4) //换下一个方向探索{e.di++;Push(stack,e);NextPos(e.seat,e.di); //设当前位置是该新方向上的相邻块,1代表向右,2代表向下,3代表向左,4代表向上。
c语言走迷宫代码
![c语言走迷宫代码](https://img.taocdn.com/s3/m/14ba5a5c571252d380eb6294dd88d0d233d43cdb.png)
c语言走迷宫代码以下是一个简单的 C 语言走迷宫代码示例,使用了递归算法: ```c#include <stdio.h>#include <stdlib.h>int main() {int size, row, col, choice, i, j;maze[0][0] = "*";maze[1][0] = "O";maze[0][1] = "O";maze[1][1] = "*";size = 2;row = 2;col = 2;while (1) {printf("Enter row: ");scanf("%d", &row);printf("Enter col: ");scanf("%d", &col);if (row < 0 || row > size || col < 0 || col > size) { printf("Invalid row or col. Try again.");}if (maze[row][col] != "*") {printf("Current cell is free. Try again. ");continue;}maze[row][col] = "X";printf("Enter a choice (1-6): ");scanf("%d", &choice);switch (choice) {case 1:row++;col = 0;break;case 2:row--;col = 0;break;case 3:col++;break;col--;break;case 5:row--;col = 1;break;case 6:row++;col = 1;break;default:printf("Invalid choice. Try again. ");continue;}}printf(" maze: ");for (i = 0; i < size * size; i++) { for (j = 0; j < size; j++) {if (maze[i][j] == "*")printf("*");elseprintf("%c", maze[i][j]);}printf("");}return 0;}```该程序首先初始化了一个 2x2 的迷宫,其中 `maze[0][0]` 和`maze[1][0]` 分别标记为 `"O"` 和 `"*"`,其他地方都为空。
c语言迷宫代码
![c语言迷宫代码](https://img.taocdn.com/s3/m/d8e0e38cfc0a79563c1ec5da50e2524de518d00b.png)
c语言迷宫代码C语言迷宫代码是指用C语言编写的程序,用于生成和解决迷宫问题的算法。
迷宫通常由一个矩形网格组成,其中包含墙壁和通道。
目标是找到从迷宫的起点到终点的路径,同时避开墙壁。
下面是一个简单的示例代码,用于生成迷宫:```c#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#define ROWS 10#define COLS 10typedef struct {int x;int y;} Point;void generateMaze(int maze[ROWS][COLS]) {for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {if (i % 2 == 0 || j % 2 == 0) { maze[i][j] = 1; // 墙壁} else {maze[i][j] = 0; // 通道}}}}void printMaze(int maze[ROWS][COLS]) {for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {printf('%d ', maze[i][j]);}printf('');}}int main() {int maze[ROWS][COLS];generateMaze(maze);printMaze(maze);return 0;}```在上面的代码中,我们使用一个二维数组来表示迷宫。
数组中的值为1表示墙壁,值为0表示通道。
使用generateMaze函数,我们将迷宫的墙壁和通道初始化为适当的值。
然后使用printMaze函数打印迷宫。
通过运行上面的代码,我们可以得到一个简单的迷宫的表示:```1 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 11 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 11 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 11 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 11 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 1```当然,上述代码只是生成了一个简单的迷宫,还没有解决迷宫问题。
C语言数据结构之迷宫问题
![C语言数据结构之迷宫问题](https://img.taocdn.com/s3/m/e682eb2442323968011ca300a6c30c225901f0db.png)
C语⾔数据结构之迷宫问题本⽂实例为⼤家分享了数据结构c语⾔版迷宫问题栈实现的具体代码,供⼤家参考,具体内容如下程序主要参考⾃严蔚敏⽼师的数据结构c语⾔版,在书中程序的⼤体框架下进⾏了完善。
关于迷宫问题的思路可查阅原书。
#include<iostream>using namespace std;#define MAXSIZE 10typedef int Status;typedef struct{int x;int y;}Postype;typedef struct{int ord;Postype seat;int dir;}SElemType;//栈的元素类型typedef struct{//SElemType data[MAXSIZE];SElemType* top;SElemType* base;}Stack;//栈的结构类型typedef struct{char arr[MAXSIZE][MAXSIZE];}MAZETYPE;//迷宫结构体MAZETYPE maze;void InitMaze(){maze.arr[0][0] = maze.arr[0][1] = maze.arr[0][2] = maze.arr[0][3] = maze.arr[0][4] = maze.arr[0][5] = maze.arr[0][6] = maze.arr[0][7] = maze.arr[0][8] = maze.arr[0][9] = '1'; maze.arr[1][0] = maze.arr[1][3] = maze.arr[1][7] = maze.arr[1][9] = '1';maze.arr[1][1] = maze.arr[1][2] = maze.arr[1][4] = maze.arr[1][5] = maze.arr[1][6] = maze.arr[1][8] = '0';maze.arr[2][0] = maze.arr[2][3] = maze.arr[2][7] = maze.arr[2][9] = '1';maze.arr[2][1] = maze.arr[2][2] = maze.arr[2][4] = maze.arr[2][5] = maze.arr[2][6] = maze.arr[2][8] = '0';maze.arr[3][0] = maze.arr[3][5] = maze.arr[3][6] = maze.arr[3][9] = '1';maze.arr[3][1] = maze.arr[3][2] = maze.arr[3][3] = maze.arr[3][4] = maze.arr[3][7] = maze.arr[3][8] = '0';maze.arr[4][0] = maze.arr[4][2] = maze.arr[4][3] = maze.arr[4][4] = maze.arr[4][9] = '1';maze.arr[4][1] = maze.arr[4][5] = maze.arr[4][6] = maze.arr[4][7] = maze.arr[4][8] = '0';maze.arr[5][0] = maze.arr[5][4] = maze.arr[5][9] = '1';maze.arr[5][1] = maze.arr[5][2] = maze.arr[5][3] = maze.arr[5][5] = maze.arr[5][6] = maze.arr[5][7] = maze.arr[5][8] = '0';maze.arr[6][0] = maze.arr[6][2] = maze.arr[6][6] = maze.arr[6][9] = '1';maze.arr[6][1] = maze.arr[6][3] = maze.arr[6][4] = maze.arr[6][5] = maze.arr[6][7] = maze.arr[6][8] = '0';maze.arr[7][0] = maze.arr[7][2] = maze.arr[7][3] = maze.arr[7][4] = maze.arr[7][6] = maze.arr[7][9] = '1';maze.arr[7][1] = maze.arr[7][5] = maze.arr[7][7] = maze.arr[7][8] = '0';maze.arr[8][0] = maze.arr[8][1] = maze.arr[8][9] = '0';maze.arr[8][2] = maze.arr[8][3] = maze.arr[8][4] = maze.arr[8][5] = maze.arr[8][6] = maze.arr[8][7] = maze.arr[8][8] = '0';maze.arr[9][0] = maze.arr[9][1] = maze.arr[9][2] = maze.arr[9][3] = maze.arr[9][4] = maze.arr[9][5] = maze.arr[9][6] = maze.arr[9][7] = maze.arr[9][8] = maze.arr[9][9] = '1'; }Status initStack(Stack &s){s.base = (SElemType*)malloc(MAXSIZE*sizeof(SElemType));if (!s.base) return 0;s.top = s.base;return 1;}void Push(Stack &s, SElemType e){*s.top++ = e;}void Pop(Stack &s, SElemType &e){e = *--s.top;}Status StackEmpty(Stack &s){if (s.top == s.base) return 1;else return 0;}Status Pass(Postype curpos){if (maze.arr[curpos.x][curpos.y] == '0')return 1;else return 0;}void Foot(Postype curpos){maze.arr[curpos.x][curpos.y] = '*';}void MarkPrint(Postype curpos){maze.arr[curpos.x][curpos.y] = '!';}Status StructCmp(Postype a, Postype b){if (a.x = b.x&&a.y == b.y) return 1;else return 0;}//下⼀个位置Postype NextPos(Postype CurPos, int Dir){Postype ReturnPos;switch (Dir){case 1:ReturnPos.x = CurPos.x;ReturnPos.y = CurPos.y + 1;break;case 2:ReturnPos.x = CurPos.x + 1;ReturnPos.y = CurPos.y;break;case 3:ReturnPos.x = CurPos.x;ReturnPos.y = CurPos.y - 1;break;case 4:ReturnPos.x = CurPos.x - 1;ReturnPos.y = CurPos.y;break;}return ReturnPos;}Status MazePath(Postype start, Postype end) {Stack s;SElemType e;initStack(s);Postype curpos = start;int curstep = 1;do{if (Pass(curpos)){Foot(curpos);e = { curstep, curpos, 1 };Push(s, e);if (StructCmp(curpos, end)) return 1;curpos = NextPos(curpos, 1);curstep++;}else{if (!StackEmpty(s)){Pop(s, e);while (e.dir ==4 &&!StackEmpty(s)){MarkPrint(e.seat); Pop(s, e);}if (e.dir < 4 && !StackEmpty(s)){e.dir++;Push(s, e);curpos = NextPos(e.seat, e.dir);}}}} while (!StackEmpty(s));return 0;}int main(){InitMaze();Postype s, e;s.x = s.y = 1;e.x = e.y = 8;if (MazePath(s, e))printf("迷宫成功解密!\n");elseprintf("解密失败\n");for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){printf("%c ", maze.arr[i][j]);}printf("\n");}cout << "-=================================" << endl;for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){if (maze.arr[i][j] == '*' || maze.arr[i][j] == '!')printf("%c ", maze.arr[i][j]);else cout << " ";}printf("\n");}}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
数据结构老鼠迷宫VC源程序
![数据结构老鼠迷宫VC源程序](https://img.taocdn.com/s3/m/246820b5767f5acfa0c7cd7c.png)
#include〈stdio。
h〉#include〈string.h>#include〈stdlib。
h>#define for if(0);else for#define max 300char map[max][max];int cross,vertical;//老鼠的横纵坐标int time;typedef struct{int x,y;int step;}point;point s;point queue[100*max];int a[max][max];int dx[4]={0,1,0,-1};int dy[4]={1,0,—1,0};int value_place(point tmp, int i,int row, int col){if(tmp。
x+dx[i]〉=0 &&tmp。
x+dx[i]〈row &&tmp。
y+dy[i]〉=0 &&tmp。
y+dy[i]〈col &&map[tmp.x+dx[i]][tmp。
y+dy[i]]!='*’&&a[tmp.x+dx[i]][tmp.y+dy[i]]==0) return 1;return 0;}int Bfs(point start, int row, int col){int i,front=0,tail=1;point tmp;tmp =start;queue[0]= tmp;queue[0]。
step = 0;while(front != tail){for(i=0; i〈4;i++){if(value_place(tmp,i,row,col)){if(map[tmp.x+dx[i]][tmp.y+dy[i]]==’C')return queue[front]。
step+1;queue[tail].x = tmp.x+dx[i];queue[tail].y = tmp。
用c语言实现迷宫求解完美源代码
![用c语言实现迷宫求解完美源代码](https://img.taocdn.com/s3/m/b87bba2b7dd184254b35eefdc8d376eeaeaa17c3.png)
用c语言实现迷宫求解完美源代码#include#include#include#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2#define UNDERFLOW -2typedef int Status;//-----栈开始-----typedef struct{//迷宫中r行c列的位置int r;int c;}PostType;//坐标位置类型typedef struct{int ord;// 当前位置在路径上的序号PostType seat;// 当前坐标int di;// 从此通块走向下一通块的“方向”}SElemType;// 栈的元素类型//定义链式栈的存储结构struct LNode{SElemType data;//数据域struct LNode *next;//指针域};struct LStack{struct LNode *top;//栈顶指针};Status InitStack(LStack &s)//操作结果:构造一个空栈S {struct LNode *p;p=(LNode *)malloc(sizeof(LNode));if(!p){printf("分配失败,退出程序");exit(ERROR);}s.top=p;p->next=NULL;return OK;}Status StackEmpty(LStack s)//若栈s为空栈,则返回TRUE,否则FALSE{if(s.top->next==NULL) return TRUE;return FALSE;}Status Push(LStack &s,SElemType e)//插入元素e成为新的栈顶元素{struct LNode *p;p=(LNode *)malloc(sizeof(LNode));if(!p) exit(OVERFLOW);s.top->data=e;p->next=s.top;s.top=p;return OK;}Status Pop(LStack &s,SElemType &e)//删除s的栈顶元素,并且用e返回其值{struct LNode *p;if(!(s.top->next)) exit(UNDERFLOW);p=s.top;s.top=p->next;e=s.top->data;free(p);return OK;}Status DestroyStack(LStack &s)//操作结果:栈s被销毁{struct LNode *p;p=s.top;while(p){s.top=p->next;free(p);p=s.top;}return OK;}//-----栈结束------//-----迷宫开始-------#define MAXLEN 10// 迷宫包括外墙最大行列数typedef struct{int r;int c;char adr[MAXLEN][MAXLEN];// 可取' ''*' '@' '#'}MazeType;// 迷宫类型Status InitMaze(MazeType&maze){// 初始化迷宫,成功返回TRUE,否则返回FALSE int m,n,i,j;printf("输入迷宫行数和列数(包括了外墙): ");scanf("%d%d",&maze.r,&maze.c); // 输入迷宫行数和列数for(i=0;i<=maze.c+1;i++){// 迷宫行外墙maze.adr[0][i]='#';maze.adr[maze.r+1][i]='#';}//forfor(i=0;i<=maze.r+1;i++){// 迷宫列外墙maze.adr[i][0]='#';maze.adr[i][maze.c+1]='#';}for(i=1;i<=maze.r;i++)for(j=1;j<=maze.c;j++)maze.adr[i][j]=' ';// 初始化迷宫printf("输入障碍的坐标((-1 -1)结束): ");scanf("%d%d",&m,&n);// 接收障碍的坐标while(m!=-1){if(m>maze.r || n>maze.c)// 越界exit(ERROR);maze.adr[m][n]='#';// 迷宫障碍用#标记printf("输入障碍的坐标((-1,-1)结束): ");scanf("%d%d",&m,&n);}//whilereturn OK;}//InitMazeStatus Pass(MazeType maze,PostType curpos){// 当前位置可同则返回TURE,否则返回FALSEif(maze.adr[curpos.r][curpos.c]==' ')// 可通return TRUE;elsereturn FALSE;}//PassStatus FootPrint(MazeType &maze,PostType curpos) {// 若走过并且可通,则返回TRUE,否则返回FALSE maze.adr[curpos.r][curpos.c]='*';//"*"表示可通return OK;}//FootPrintPostType NextPos(PostType &curpos,int i){// 指示并返回下一位置的坐标PostType cpos;cpos=curpos;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;default: exit(ERROR);}return cpos;}//NextposStatus MarkPrint(MazeType &maze,PostType curpos) {// 曾走过,但不是通路标记,并返回OKmaze.adr[curpos.r][curpos.c]='@';//"@" 表示曾走过但不通return OK;}//MarkPrintStatus MazePath(MazeType &maze,PostType start,PostType end){// 若迷宫maze存在通路,则求出一条同路放在栈中,并返回TRUE,否则返回FALSE struct LStack S;PostType curpos;int curstep;// 当前序号,1,2,3,4分别表示东南西北方向SElemType e;InitStack(S);curpos=start; //设置"当前位置"为"入口位置"curstep=1;// 探索第一位printf("以三元组形式表示迷宫路径:\n");do{if(Pass(maze,curpos)){// 当前位置可以通过FootPrint(maze,curpos);// 留下足迹e.ord=curstep;e.seat=curpos;e.di=1;printf("%d %d %d-->",e.seat.r,e.seat.c,e.di);Push(S,e);// 加入路径if(curpos.r==end.r&&curpos.c==end.c)if(!DestroyStack(S))// 销毁失败exit(OVERFLOW);elsereturn TRUE; // 到达出口else{curpos=NextPos(curpos,1);// 下一位置是当前位置的东邻curstep++;// 探索下一步}//else}//ifelse{// 当前位置不通时if(!StackEmpty(S)){Pop(S,e);while(e.di==4&& !StackEmpty(S)){MarkPrint(maze,e.seat);Pop(S,e);// 留下不能通过的标记,并退一步}//whileif(e.di < 4){e.di++;// 换一个方向探索Push(S,e);curpos=NextPos(e.seat,e.di);// 设定当前位置是该方向上的相邻printf("%d %d %d-->",e.seat.r,e.seat.c,e.di);}//if}//if}//else}while(!StackEmpty(S));if(!DestroyStack(S))// 销毁栈exit(OVERFLOW);elsereturn FALSE;}//MazePathvoid PrintMaze(MazeType &maze){// 将标记路径信息的迷宫输出到终端int i,j;printf("\n输出迷宫(*表示通路):\n\n");printf("");for(i=0;i<=maze.r+1;i++)// 打印列数名printf("%4d",i);printf("\n\n");for(i=0;i<=maze.r+1;i++){printf("%2d",i);// 打印行名for(j=0;j<=maze.c+1;j++)printf("%4c",maze.adr[i][j]);// 输出迷宫当前位置的标记printf("\n\n");}}//PrintMazeint main(){// 主函数MazeType maze;PostType start,end;char cmd;do{printf("-------创建迷宫--------\n");if(!InitMaze(maze)){printf("Initialization errors\n");exit(OVERFLOW);// 初始化失败}do{// 输入迷宫入口坐标printf("\n输入迷宫入口坐标: ");scanf("%d%d",&start.r,&start.c);if(start.r>maze.r ||start.c>maze.c){printf("\nBeyond the maze\n"); continue;}}while(start.r>maze.r ||start.c>maze.c);do{// 输入迷宫出口坐标printf("\n输入迷宫出口坐标: ");scanf("%d%d",&end.r,&end.c);if(end.r>maze.r ||end.c>maze.c){printf("\nBeyond the maze\n"); continue;}}while(end.r>maze.r ||end.c>maze.c);if(!MazePath(maze,start,end))//迷宫求解printf("\nNo path from entranceto exit!\n"); elsePrintMaze(maze);// 打印路径printf("\n需要继续创建新的迷宫吗?(y/n): "); scanf("%s",&cmd);}while(cmd=='y' || cmd=='Y');}。
数据结构迷宫源代码
![数据结构迷宫源代码](https://img.taocdn.com/s3/m/2a7dc9a3915f804d2a16c14e.png)
#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <string.h>#include <time.h>#define OK 1#define ERROR 0#define NULL 0#define OVERFLOW -2#define STACK_INIT_SIZE 100#define STACKINCREMENT 10//栈的顺序存储表示typedef struct{int x; /*列*/int y; /*行*/}PosType; //坐标位置类型typedef struct{int ord; //通道块在路径上的序号PosType seat; //通道块在迷宫中的坐标位置int di; //从此通道块走向下一通道块的方向}SElemType; //栈的元素类型typedef struct {SElemType *base;SElemType *top;int stacksize; //当前已分配的存储空间,以元素为单位}SqStack;//基本操作int 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;}//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR int GetTop(SqStack *S,SElemType *e){if(S->top==S->base) return ERROR;*e=*(S->top-1);return OK;}int Push(SqStack *S,SElemType *e)//插入元素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;}//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERRORint Pop(SqStack *S,SElemType *e){if(S->top==S->base) return ERROR;*e=*--S->top;return OK;}int StackEmpty(SqStack *S){return(S->top==S->base) ;}//迷宫程序typedef struct {int lie; /*列数*/int hang; /*行数*/char a[999][999];}MazeType; /*迷宫类型*//*随机生成迷宫*/int generatemaze( MazeType *maze){int i,j;maze->a[0][0]=2;maze->a[++maze->hang][++maze->lie]=3;/*设置外墙*/maze->a[0][maze->lie]='!';maze->a[maze->hang][0]='!';for(j=1;j<maze->lie;j++){maze->a[0][j]='!';maze->a[maze->hang][j]='!';}for(i=1;i<maze->hang;i++){maze->a[i][0]='!';maze->a[i][maze->lie]='!';}srand((unsigned)time( NULL ));rand();for(i=1; i <maze->hang; i++)for(j=1;j<maze->lie;j++){if (rand()>=RAND_MAX/4) maze->a[i][j] =' '; //' ' 暗示出路else maze->a[i][j] ='!'; //'!'暗示无出路}return OK;}int Pass(MazeType *maze, PosType curpos ) //判断当前位置可否通过{if ((curpos.x < 1) || (curpos.x >= maze->lie)) return ERROR;if ((curpos.y < 1) || (curpos.y >= maze->hang)) return ERROR;if (maze->a[curpos.y][curpos.x]==' ') return OK;else return ERROR;}int FootPrint(MazeType *maze,PosType curpos) //留下足迹{maze->a[curpos.y][curpos.x]='*';return OK;}int MarkPrint(MazeType *maze,PosType curpos) //留下不能通过的标记{maze->a[curpos.y][curpos.x]='@';return OK;}PosType NextPos(PosType curpos,int di) //返回当前位置的下一位置{PosType pos=curpos;switch(di){case 1: //右东pos.x++;break;case 2: //下南pos.y++;break;case 3: //左西pos.x--;break;case 4: //上北pos.y--;break;}return pos;}//若迷宫有解,则求得一条存放在栈中(从栈底到栈顶),并返回OK,否则返回ERROR int MazePath(MazeType *maze,PosType start,PosType end){PosType curpos;SqStack *S=(SqStack *)malloc(sizeof(SqStack));InitStack(S);SElemType *e;e=(SElemType *)malloc(sizeof(SElemType));curpos=start; //设定当前位置为入口位置int curstep = 1; //探索第一步do{if(Pass(maze,curpos)) //当前位置可通过{FootPrint(maze,curpos);e->ord=curstep;e->seat=curpos;e->di=1;Push(S,e);if(curpos.x==end.x&&curpos.y==end.y) return (OK);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=e->seat;curpos=NextPos(curpos,e->di);}}}}while(!StackEmpty(S));return ERROR;}void PrintMaze(MazeType *maze) //打印迷宫{int i,j,k,n;int c[999],d[999];for(i=0,k=0;i<=maze->hang;i++){for(j=0;j<=maze->lie;j++){printf("%c ",maze->a[i][j]);if(maze->a[i][j]=='*'){c[k]=i;d[k]=j;k++;}}printf("\n");}n=k;for(k=0;k<n;k++)printf("<%d,%d>",c[k],d[k]);printf("\n");printf("\n");}int main(){int zmg;char ch;printf(" 数据结构课程设计--迷宫问题求解\n\n");printf(" |----------------------------------------|\n");printf(" | |\n");printf(" | |\n");printf(" | |\n");printf(" | |\n");printf(" | XXXX XXXXXXXXXXXXXX |\n");printf(" | XXXXXXX |\n");printf(" |----------------------------------------|\n");getchar();do{system("cls");fflush(stdin);MazeType *maze=(MazeType *)malloc(sizeof(MazeType));//设置迷宫的长宽不含外墙printf("请输入迷宫的列数(不含外墙时):");scanf("%d",&maze->lie);printf("请输入迷宫的行数(不含外墙时):");scanf("%d",&maze->hang);generatemaze(maze);printf("随机创建迷宫\n");PrintMaze(maze);getchar();getchar();PosType start,end;start.x=1;start.y=1;end.x=maze->lie-1;end.y=maze->hang-1;zmg=MazePath(maze,start,end);if(zmg){printf("此迷宫通路为\n");PrintMaze(maze);}else printf("此迷宫无通路\n");//getchar();printf("再次尝试?(Y/N)?");scanf("%c",&ch);}while(ch=='Y'||ch=='y');return 0; }。
迷宫求解数据结构C语言版
![迷宫求解数据结构C语言版](https://img.taocdn.com/s3/m/eba80adf76eeaeaad1f33064.png)
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10typedef struct{int r;int c;}PosType;//通道块坐标typedef struct {int ord;//通道块在路径上的序号PosType seat;//通道块的坐标位置int di;//下一个探索的方向}SelemType;typedef struct {int r;int c;char adr[10][10];}MazeType;//迷宫行列,内容typedef struct {SelemType * base;SelemType * top;int stacksize;}SqStack;void InitStack(SqStack &S){S.base=(SelemType*)malloc(STACK_INIT_SIZE * sizeof(SelemType));if(!S.base) exit(0);S.top=S.base;S.stacksize=STACK_INIT_SIZE;}void 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(0);S.top=S.base+S.stacksize;//溢出重新定位S.stacksize+=STACKINCREMENT;}*S.top++=e;}void Pop(SqStack &S,SelemType &e){if(S.top==S.base) return;e=*--S.top;}void InitMaze(MazeType &maze){int i,j;maze.r=8;maze.c=8;for(i=0;i<10;i++){maze.adr[0][i]='#';maze.adr[9][i]='#';}for(i=0;i<10;i++){maze.adr[i][0]='#';maze.adr[i][9]='#';}for(i=1;i<9;i++)for(j=1;j<9;j++)maze.adr[i][j]='1';maze.adr[1][3]='#'; maze.adr[1][7]='#';maze.adr[2][3]='#'; maze.adr[2][7]='#';maze.adr[3][5]='#'; maze.adr[3][6]='#';maze.adr[4][2]='#'; maze.adr[4][3]='#';maze.adr[4][4]='#';maze.adr[5][4]='#';maze.adr[6][2]='#'; maze.adr[6][6]='#';maze.adr[7][2]='#'; maze.adr[7][3]='#'; maze.adr[7][4]='#'; maze.adr[7][6]='#';maze.adr[7][7]='#';maze.adr[8][1]='#';}int Pass(MazeType &maze,PosType &curpos){if(maze.adr[curpos.r][curpos.c]=='1')return 1;elsereturn 0;}void FootPrint(MazeType &maze,PosType curpos){maze.adr[curpos.r][curpos.c]='*';//已经走过的标记}PosType NextPos(PosType curpos,int i){PosType nextpos;nextpos=curpos;switch(i){case 1:nextpos.c+=1;break;case 2:nextpos.r+=1;break;case 3:nextpos.c-=1;break;case 4:nextpos.r-=1;break;default:printf("探寻方向错误!");}return nextpos;}void MarkPrint(MazeType &maze,PosType curpos){maze.adr[curpos.r][curpos.c]='@';//不通的标识}int StackEmpty(SqStack &S){if(S.base==S.top)return 1;elsereturn 0;}void MazePath(MazeType &maze,PosType start,PosType end) {SelemType e;SqStack S;InitStack(S);PosType curpos;int curstep;curpos=start;curstep=1;do{if(Pass(maze,curpos)){FootPrint(maze,curpos);//留下走过足迹*e.ord=curstep;e.seat=curpos;e.di=1;Push(S,e);//压入栈路径if(curpos.r==end.r&&curpos.c==end.c) return;curpos=NextPos(curpos,1);//探索下一步curstep++;}//ifelse{//当前位置不能通过if(!StackEmpty(S)){Pop(S,e);//top指针退后一下,指向上一步while(e.di==4&&!StackEmpty(S)){MarkPrint(maze,e.seat);//如果此步上下左右都探索过了且栈不为空留下不通过且返回一步Pop(S,e);}//while,用while是可以连续返回if(e.di<4){e.di++;Push(S,e);//换下一个方向探索curpos=NextPos(e.seat,e.di);//探索下一位置块}//if}//if}//else}while(!StackEmpty(S));}//mazepathvoid PrintMaze(MazeType &maze){int i,j;for(i=0;i<10;i++)printf("%4d",i);//输出行printf("\n\n");for(i=0;i<10;i++){printf("%2d",i);//输出列for(j=0;j<10;j++)printf("%4c",maze.adr[i][j]);printf("\n\n");}}void main(){MazeType maze;PosType start,end;InitMaze(maze);start.r=1; start.c=1;end.r=8; end.c=8;PrintMaze(maze);MazePath(maze,start,end);PrintMaze(maze);}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
数据结构课程设计——迷宫问题求解代码
问题描述及要求:
迷宫问题求解
输入:
第一行n,m表示迷宫大小n*m
之后有n行m列,全由01组成,0表示路,1表示墙
入口设为(0,0)
出口设为(n-1,m-1)
输出:
输出从入口到出口的所有不同解,每组解的第一行打印一个数表示第几组解,之后k行表示路径,如:
1
(0,0)
(0,1)
(1,1)
代码部分(已测试,可直接运行):
#include<cstdio>
#include<cstring>
#define N255
int n,m,cnt;
int maze[N][N],step[N][N];
struct Point{
int x,y;
}path[N*N];
int oper[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool isvaild(int x,int y){
if(x>=0&&x<n&&y>=0&&y<m&&!maze[x][y]&&!step[x][y])
return true;
return false;
}
void dfs(int x,int y,int len){
if(x==n-1&&y==m-1){
cnt++;
printf("%d\n",cnt);
for(int i=0;i<len;i++)
printf("(%d,%d)\n",path[i].x,path[i].y);
return;
}
for(int i=0;i<4;i++)
if(isvaild(x+oper[i][0],y+oper[i][1])){
Point tmp;
tmp.x=x+oper[i][0];
tmp.y=y+oper[i][1];
path[len]=tmp;
step[tmp.x][tmp.y]=1;
dfs(tmp.x,tmp.y,len+1);
step[tmp.x][tmp.y]=0;
}
}
void work(){
step[0][0]=1;
Point cur;
cur.x=0;
cur.y=0;
path[0]=cur;
dfs(0,0,1);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&maze[i][j]);
cnt=0;
memset(step,0,sizeof(step));
work();
return0;。