common.h
#include <stdlib.h> #define TURE 0 #define FALSE 1 #define OK 0 #define ERR 1 #define MEM_ALOC_ERR 3 |
stack.h
/** * 堆栈数据结构 */ #include <stdlib.h> #define STACK_INIT_SIZE 200 #define STACK_INCREMENT 20 /** * 描述在迷宫表格中的位置; * x表示第一维度 * y表示第二维度 * 注意:这里的x y使用的是数据下标表示方法,即左上角为(0,0) 右上角为(0,MAX) 左下角为(MAX,0) 右下角为(MAX, MAX) */ typedef struct tagPos { int x; int y; }Pos; /** * 堆栈元素 */ typedef struct tagStackElement { Pos pos; /* 位置 */ int direction; /* 方向 */ int step; }stackElement, *pStackElement; /** * 堆栈数据结构 */ typedef struct tagStack { pStackElement base; pStackElement top; int stackSize; }Stack; void elementCopy(pStackElement src, pStackElement des); int InitStack(Stack *s); int GetTop(Stack s, pStackElement pElement); int Push(Stack *s, pStackElement e); int Pop(Stack *s, pStackElement e); int StackEmpty(Stack *s); |
stack.c
#include <stdio.h> #include <stdlib.h> #include "Stack.h" #include "common.h" void elementCopy(pStackElement des, pStackElement src) { des->direction = src->direction; des->pos.x = src->pos.x; des->pos.y = src->pos.y; des->step = src->step; return; } int InitStack(Stack *S) { S->base = (pStackElement)malloc(STACK_INIT_SIZE * sizeof(stackElement)); if ( !S->base) exit(MEM_ALOC_ERR); S->top = S->base; S->stackSize = STACK_INIT_SIZE; return OK; }; int GetTop(Stack s, pStackElement pElement) { if (s.base == s.top) return ERR; pElement = s.top - 1; return OK; } int Push(Stack *s, pStackElement e) { if(s->top - s->base >= s->stackSize) { pStackElement ptmp = (pStackElement)realloc(s->stackSize, (s->stackSize + STACK_INCREMENT) * sizeof(stackElement)); if (!ptmp) { free(s->base); exit(MEM_ALOC_ERR); } s->base = ptmp; } elementCopy(s->top, e); s->top++; return OK; } int Pop(Stack *s, pStackElement e) { if(s->base == s->top) return ERR; s->top--; elementCopy(e, s->top); return OK; } int StackEmpty(Stack *s) { if (s->base == s->top) return TURE; else return FALSE; } void DestroyStack(Stack *s) { s->stackSize = 0; s->top = s->base; free(s->base); } |
Maze_stack.c
/* ============================================================================ Name : Maze_Stack.c Author : Glarple Version : Copyright : SpecialLove Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include "Stack.h" #include "common.h" #define MAZE_SIZE 10 int Maze[MAZE_SIZE][MAZE_SIZE] = { {0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,1,1,0,0,1,1,0}, {0,1,0,0,0,1,1,1,1,0}, {0,1,1,1,0,1,1,1,1,0}, {0,1,0,1,1,1,0,1,1,0}, {0,1,0,0,0,1,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0} }; int PosCompare(Pos first, Pos second) { if (first.x == second.x && first.y == second.y) return TURE; else return FALSE; } void PosCopy(Pos *des, Pos *src) { des->x = src->x; des->y = src->y; } void NextPos(Pos *p, int di) { switch(di) { case 0: p->y += 1; break; case 1: p->x += 1; break; case 2: p->y -= 1; break; case 3: p->x -= 1; break; default: break; } } int MazePath() { Pos start; Pos end; Stack s; Pos curPos; stackElement se; int curstep = 1; start.x = 1; start.y = 1; end.x = 8; end.y = 8; InitStack(&s); PosCopy(&curPos, &start); do { if (Maze[curPos.x][curPos.y] == 1) { Maze[curPos.x][curPos.y] = 2; se.direction = 1; se.step = curstep; PosCopy(&(se.pos), &curPos); Push(&s, &se); if (!PosCompare(curPos, end)) { return TURE; } //nextPos(&curPos, 0); curPos.y += 1; curstep += 1; } else { if(StackEmpty(&s)) { Pop(&s, &se); while(se.direction == 4 && StackEmpty(&s)) { Maze[se.pos.x][se.pos.y] = 3; Pop(&s, &se); } if (se.direction < 4){ se.direction += 1; Push(&s, &se); PosCopy(&curPos, &(se.pos)); NextPos(&curPos, se.direction -1); } } } }while(StackEmpty(&s)); return FALSE; } void setPath(Stack *s) { stackElement se; while(StackEmpty(s)) { Pop(s, &se); //printf(); Maze[se.pos.x][se.pos.y] = 9; } } void printPath() { int i, j; for(i = 0; i< MAZE_SIZE; i++) { for(j=0; j < MAZE_SIZE; j++) { printf("%d",Maze[i][j]); } printf("\n"); } } int main(void) { puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */ printPath(); MazePath(); puts("============="); printPath(); return EXIT_SUCCESS; } |