迷宫求解
2014-12-10 08:54
148 查看
抽象结构见严蔚敏版《数据结构》3.2.4迷宫求解问题。
#include<stdio.h> #include<memory.h> #include<stdlib.h> #define TRUE 1 #define STACK_INIT_SIZE 100 #define OVERFLOW -2 #define RANGE 100 #define STACKINCREMENT 10 #define OK 1 #define ERROR 0 #define FALSE 0 typedef int Status; 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; typedef struct{ Status block[RANGE][RANGE]; Status path[RANGE][RANGE]; }MazeType; Status 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; } Status Push(MazeType &maze, 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(OVERFLOW); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; maze.path[e.seat.y][e.seat.x]=1; return OK; } Status Pop (MazeType &maze, SqStack &S, SElemType &e) { if (S.top == S.base) return ERROR; e = *--S.top; maze.path[e.seat.y][e.seat.x]=0; return OK; } Status StackEmpty (SqStack S) { if (S.top == S.base) return OK; else return ERROR; } Status Pass(PosType P, MazeType maze) { return !maze.block[P.y][P.x]; } void FootPrint(PosType P, MazeType &maze) { maze.block[P.y][P.x] = 1; return; } PosType NextPos(PosType curpos, int di) { PosType temp; switch (di) { case 1: temp.x = curpos.x + 1; temp.y = curpos.y; break; case 2: temp.x = curpos.x; temp.y = curpos.y + 1; break; case 3: temp.x = curpos.x - 1; temp.y = curpos.y; break; case 4: temp.x = curpos.x; temp.y = curpos.y - 1; break; default: break; } return temp; } void MarkPrint(PosType p, MazeType &maze) { maze.block[p.y][p.x] = 1; return; } Status posCompare(PosType a, PosType b) { if (a.x == b.x && a.y == b.y) return OK; else return ERROR; } Status MazePath(MazeType &maze, PosType start, PosType end) { SqStack S; InitStack(S); PosType curpos = start; int curstep = 1; SElemType e; do { if (Pass(curpos, maze)) { FootPrint(curpos, maze); e = (SElemType){curstep, curpos, 1}; Push(maze, S, e); if (posCompare(curpos, end)) { return TRUE; } curpos = NextPos(curpos, 1); curstep++; } else { if (!StackEmpty(S)) { Pop(maze, S, e); while (e.di == 4 && !StackEmpty(S)) { MarkPrint(e.seat, maze); Pop(maze, S, e); } if (e.di < 4) { e.di++; Push(maze, S, e); curpos = NextPos(e.seat, e.di); } } } }while (!StackEmpty(S)); return FALSE; } int main() { /*test code begin*/ MazeType test = { { {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} } }; memset(test.path, 0, sizeof(test.path)); PosType posStart = PosType{1,1}; PosType posStop = PosType{8,8}; MazePath(test, posStart, posStop); for (int i = 0; i <= 10; i++) { for (int j = 0; j <= 10; j++) printf("%d\t", test.path[i][j]); printf("\n"); } /*test code end*/ return 0; }
相关文章推荐
- 栈求解迷宫
- 【数据结构实验】编制一个程序求解迷宫通路
- 数据结构(C语言版)---第三章栈和队列 3.2.4 迷宫求解
- C语言迷宫求解(完结版)
- 题目1335:闯迷宫( BFS在求解最短路径或者最短步数上有很多的应用)
- 经典算法<一>迷宫问题 2.单条路径 BFS求解 C++实现
- 栈的应用举例——迷宫求解
- 迷宫求解
- 运用栈求解迷宫问题C/C++
- 栈解决迷宫求解
- [数据结构]求解迷宫最短路径问题
- 数据结构:栈的应用(迷宫的求解)
- 队列求解迷宫路径
- 迷宫求解
- algo3-4-3.c 利用非循环顺序队列采用广度搜索法求解迷宫问题(一条路径)
- 迷宫求解(使用队列
- 迷宫求解无敌版(递归调用法)
- 利用栈实现简单的求解迷宫程序
- 迷宫求解问题-递归(栈的应用)
- 迷宫求解