严蔚敏 数据结构 课本中 栈应用 走迷宫 C语言 完整版
来源:互联网 发布:淘宝最迟几天确认收货 编辑:程序博客网 时间:2024/05/06 07:57
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define STACK_INIT_SIZE 100
- #define INCREMENT 10
- /* typedef int ElemType; */
- typedef struct{
- int r;
- int c;
- }PosType;
- typedef struct{
- int ord; /* 通道块在路径上的序号 */
- PosType seat; /* 通道块在迷宫中的“坐标位置” */
- int di; /* 从此通道块走向下一个通道块的“方向” */
- }ElemType;
- typedef struct{
- int arr[10][10];
- }MazeType;
- typedef struct SqStack
- {
- ElemType *base;
- ElemType *top;
- int size;
- }SqStack;
- int initStack(SqStack *s)
- {
- s->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType) );
- if(!s->base)
- return -1;
- s->top = s->base;
- s->size = STACK_INIT_SIZE;
- return 0;
- }
- int getTop(SqStack s,ElemType *e)
- {
- if(s.base!=s.top)
- *e = *(s.top-1);
- return -1;
- }
- int push(SqStack *s, ElemType e)
- {
- if(s->top - s->base >= s->size)
- {
- s->base = (ElemType *)realloc(s->base, (s->size+INCREMENT)*sizeof(ElemType));
- if(!s->base)
- return -1;
- s->top = s->base+s->size;
- s->size += INCREMENT;
- }
- *s->top++ = e;
- return 0;
- }
- int pop(SqStack *s,ElemType *e)
- {
- if(s->top == s->base)
- return -1;
- *e = *(--s->top);
- return 0;
- }
- int isEmpty(SqStack *s)
- {
- if(s->base == s->top)
- return 1;
- else
- return 0;
- }
- int clearStack(SqStack *s)
- {
- ElemType e;
- if(!s) return -1;
- if(s->base == s->top) return 0;
- while(!isEmpty(s))
- {
- pop(s,&e);
- }
- return 0;
- }
- int pass(MazeType MyMaze, PosType CurPos);
- void footPrint(MazeType &MyMaze, PosType CurPos);
- void markPrint(MazeType &MyMaze, PosType CurPos);
- PosType nextPos(PosType CurPos, int Dir);
- int pass( MazeType MyMaze,PosType CurPos) {
- if (MyMaze.arr[CurPos.r][CurPos.c]==' ')
- return 1; // 如果当前位置是可以通过,返回1
- else return 0; // 其它情况返回0
- }
- void footPrint(MazeType &MyMaze,PosType CurPos) {
- MyMaze.arr[CurPos.r][CurPos.c]='*';
- }
- void markPrint(MazeType &MyMaze,PosType CurPos) {
- MyMaze.arr[CurPos.r][CurPos.c]='!';
- }
- PosType nextPos(PosType CurPos, int Dir) {
- PosType ReturnPos;
- switch (Dir) {
- case 1:
- ReturnPos.r=CurPos.r;
- ReturnPos.c=CurPos.c+1;
- break;
- case 2:
- ReturnPos.r=CurPos.r+1;
- ReturnPos.c=CurPos.c;
- break;
- case 3:
- ReturnPos.r=CurPos.r;
- ReturnPos.c=CurPos.c-1;
- break;
- case 4:
- ReturnPos.r=CurPos.r-1;
- ReturnPos.c=CurPos.c;
- break;
- }
- return ReturnPos;
- }
- /* 迷宫函数 */
- /* 判断是否存在一条从开口到结尾的路径 */
- int mazePath(MazeType &maze, PosType start, PosType end)
- {
- SqStack *s = (SqStack *)malloc(sizeof(SqStack));
- initStack(s);
- PosType curpos = start; // 设定"当前位置"为"入口位置"
- ElemType e;
- int curstep = 1; // 探索第一步
- do {
- if (pass(maze,curpos)) { // 当前位置可通过,即是未曾走到过的通道块
- footPrint(maze,curpos); // 留下足迹
- e.di =1;
- e.ord = curstep;
- e.seat= curpos;
- push(s,e); // 加入路径
- if (curpos.r == end.r && curpos.c==end.c)
- return 0; // 到达终点(出口)
- curpos = nextPos(curpos, 1); // 下一位置是当前位置的东邻
- curstep++; // 探索下一步
- } else { // 当前位置不能通过
- if (!isEmpty(s)) {
- pop(s,&e);
- while (e.di==4 && !isEmpty(s)) {
- markPrint(maze,e.seat);
- pop(s,&e); // 留下不能通过的标记,并退回一步
- } // while
- if (e.di<4) {
- e.di++;
- push(s, e); // 换下一个方向探索
- curpos = nextPos(e.seat, e.di); // 当前位置设为新方向的相邻块
- } // if
- } // if
- } // else
- } while (!isEmpty(s) );
- return -1;
- } // MazePath
- int main()
- {
- //printf("Hello world!");
- return 0;
- }
- 原文链接:http://blog.csdn.net/daringpig/article/details/7657407
0 0
- 严蔚敏 数据结构 课本中 栈应用 走迷宫 C语言 完整版
- 严蔚敏 数据结构 课本中 栈应用 走迷宫 C语言 完整版 源代码和注释 可直接执行
- 栈应用-走迷宫
- C语言走迷宫源代码
- c语言 走迷宫问题
- 栈应用2:走迷宫
- C语言算法之老鼠走迷宫
- C语言小游戏-走迷宫
- 用C语言实现走迷宫
- 数据结构-栈应用-迷宫
- 【数据结构】走迷宫实现
- 数据结构-栈/c++/迷宫
- 数据结构 C语言 双向链栈 迷宫问题
- 学习笔记------数据结构(C语言版)栈应用 迷宫寻路
- 递归算法求老鼠走迷宫(C语言)
- 从算法学起C语言--老鼠走迷宫
- 数据结构 C语言 队列 迷宫问题
- C语言 迷宫问题求解(顺序栈应用示例)
- Java获取当前的年月
- IPv4 + NAT
- 关于ubuntu下Android工作空间出现android-support-v7-appcompat
- OpenStack最新版本Folsom架构解析(2)
- 如何导入导出MySQL数据库
- 严蔚敏 数据结构 课本中 栈应用 走迷宫 C语言 完整版
- hdu1753大明A+B
- fedora18安装ssh
- imagebutton添加.9图片文件
- hdu 1000 A + B Problem
- 华丽转身——浅识J2EE十三标准
- java package 重命名时注意事项
- wust April Chanllenge 2014 C题 poj1751 Highways
- Struts2中的动态链接标签--->动态传值【转】