数据结构迷宫问题的C 代码
数据结构迷宫问题源代码

数据结构迷宫问题源代码#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++代码迷宫问题是一个经典的寻路问题,目标是从迷宫的入口找到出口的路径。
栈是一个常用的数据结构,可以用来实现迷宫问题的解决方法。
下面是使用栈算法解决迷宫问题的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语言解决数据结构(严蔚敏)中关于迷宫的问题。
程序不仅实现迷宫路径查找,还实现文字描述路径功能可以直接粘贴到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++代码

数据结构课程设计——迷宫问题求解代码问题描述及要求:迷宫问题求解输入:第一行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语言版)-源代码(直接运行)

#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");}}}。
数据结构迷宫问题源代码

#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语言代码

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语言版非循环顺序队列求解迷宫问题利用非循环顺序队列采用广度搜索法求解迷宫问题(一条路径)编译环境: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。
- 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;。