C语言数据结构之迷宫问题
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
本文实例为大家分享了数据结构c语言版迷宫问题栈实现的具体代码,供大家参考,具体内容如下
程序主要参考自严蔚敏老师的数据结构c语言版,在书中程序的大体框架下进行了完善。关于迷宫问题的思路可查阅原书。
#include<iostream> using namespace std; #define MAXSIZE 10 typedef 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"); else printf("解密失败\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"); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 04-02c语言函数调用后清空内存 c语言调用函数删除字符
- 04-02c语言的正则匹配函数 c语言正则表达式函数库
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言中对数函数的表达式 c语言中对数怎么表达
- 04-02c语言用函数写分段 用c语言表示分段函数
- 04-02c语言编写函数冒泡排序 c语言冒泡排序法函数
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段函数
- 04-02C语言中怎么打出三角函数 c语言中怎么打出三角函数的值
- 04-02c语言调用函数求fibo C语言调用函数求阶乘
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-11ajax实现页面的局部加载
- 04-02jquery与jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10SublimeText编译C开发环境设置
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10C#中split用法实例总结
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文