数据结构 C语言 双向链栈 迷宫问题
来源:互联网 发布:自动生成迷宫算法 编辑:程序博客网 时间:2024/05/26 02:55
【问题描述】
以一个mXn的矩阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。
【任务要求】
实现链栈求解迷宫从入口到出口的一条可行通路。
【测试数据】
迷宫的测试数据如下:左上角(0,1)为入口,右下角(8,9)为出口。
#include<stdio.h>#include<stdlib.h>int count = 0;typedef struct { int x, y;}position;typedef struct node{ position data; struct node *next, *pre;}linkNode;typedef struct { linkNode *head; linkNode *top;}linkStack;int initStack(linkStack *s){ s->head = (linkNode*)malloc(sizeof(linkNode)); s->top = s->head; s->top->next = NULL; s->top->pre = NULL;}int pushStack(linkStack *s, position value){ linkNode *p; p = (linkNode*)malloc(sizeof(linkNode)); s->top->data = value; p->next = s->top->next; p->pre = s->top; s->top->next = p; s->top = p; count++; return 0;}int popStack(linkStack *s, position *nowPos){ s->top = s->top->pre; free(s->top->next); count--; *nowPos = s->top->pre->data;}int main(){ linkStack s; linkNode *p; position nowPos, startPos, endPos; int i, m, n, j; int **map; initStack(&s); printf("请输入行列数\n"); scanf("%d%d", &m, &n); map = (int**)malloc(sizeof(int)*m); for (i = 0; i < m; i++) { map[i] = (int*)malloc(sizeof(int)*n); } printf("请输入地图:\n"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { scanf("%d", &map[i][j]); } } printf("\n"); printf("请输入开始位置和结束位置:\n"); scanf("%d %d", &startPos.x, &startPos.y); scanf("%d %d", &endPos.x, &endPos.y); nowPos = startPos; pushStack(&s, nowPos); while ((nowPos.x != endPos.x) || (nowPos.y != endPos.y)) { if (0 == map[nowPos.x + 1][nowPos.y]) { nowPos.x += 1; map[nowPos.x][nowPos.y] = 2; pushStack(&s, nowPos); } else if (0 == map[nowPos.x][nowPos.y - 1]) { nowPos.y -= 1; map[nowPos.x][nowPos.y] = 2; pushStack(&s, nowPos); } else if (0 == map[nowPos.x - 1][nowPos.y]) { nowPos.x -= 1; map[nowPos.x][nowPos.y] = 2; pushStack(&s, nowPos); } else if (0 == map[nowPos.x][nowPos.y + 1]) { nowPos.y += 1; map[nowPos.x][nowPos.y] = 2; pushStack(&s, nowPos); } else { map[nowPos.x][nowPos.y] = 2; popStack(&s, &nowPos); } } printf("路径为:\n"); for (i = 0, p = s.head; i < count; i++,p = p->next) { printf("%d,%d\n", p->data.x, p->data.y); } system("pause"); return 0;}
阅读全文
0 0
- 数据结构 C语言 双向链栈 迷宫问题
- 数据结构 C语言 队列 迷宫问题
- 数据结构(C++) 栈-迷宫问题
- C语言数据结构之双向链表
- C语言数据结构----双向链表
- C 语言 数据结构之双向链表
- 迷宫问题的通用解法C语言数据结构实现
- 数据结构-栈/c++/迷宫
- C语言链栈以及回溯算法解决迷宫问题
- 迷宫问题C语言实现
- 迷宫问题C语言解法
- 迷宫问题--C语言实现
- c语言之迷宫问题
- c语言 走迷宫问题
- 数据结构C语言实现系列——双向链表
- (C语言)双向链表实现案例(数据结构六)
- 数据结构——双向链表(C语言)
- C语言数据结构--双向链表的学习
- golang以插件的方式加载golang动态库
- 关于lhgdialog二级弹窗问题
- Android图片压缩参数Bitmap.Config.RGB_565,Bitmap.Config.ARGB_8888等
- 走进Spring Boot世界
- Kotlin-30.相等性(Equality)
- 数据结构 C语言 双向链栈 迷宫问题
- go拾遗--使用竞争检测器标志来编译程序
- linux安装mysql后修改密码
- python学习(字符串转换)
- 24.职责链模式
- c++类模板的声明与定义
- 考试题/(ㄒoㄒ)/~~
- 25.中介模式
- 19. Remove Nth Node From End of List-python