1. 程式人生 > >棧的應用,最簡單迷宮

棧的應用,最簡單迷宮

棧的操作 : Stack.h

#pragma once
#include<assert.h>
#include"Maze.h"
#define MAX_SIZE 100
extern Pos;
typedef Pos DataType;
typedef struct stack
{
DataType _data[MAX_SIZE];
int _top;


}SqStack;
SqStack s;
void SqStackInit(SqStack* Stack);
void Push(SqStack* Stack,DataType data);
void pop(SqStack*Stack);
Pos StackTop(SqStack*Stack);
int StackTopMaxSize(SqStack* Stack);

棧的實現:Stack.c

#include"Stack.h"                                                                                                                                                                                    
#include"Maze.h"


void SqStackInit(SqStack * Stack)
{
assert(Stack);
Stack->_top = 0;
}


void Push(SqStack * Stack,DataType data)
{
assert(Stack);
if(Stack->_top == MAX_SIZE)
{
printf("滿了");
return;
}


Stack->_data[Stack->_top] = data;
++Stack->_top;
}
void Pop(SqStack*Stack)
{
assert(Stack);
if(Stack->_top == 0)
{
printf("棧頂為空");
return;
}
(Stack->_top)--;
}
Pos StackTop(SqStack* Stack)
{
assert(Stack);
return Stack->_data[Stack->_top - 1];


}


int StackTopMaxSize()
{
return MAX_SIZE;
}

迷宮的宣告:Maze.h

#pragma once
#include<stdio.h>
#include<assert.h>


#define N 6
typedef struct Pos
{
int _x;
int _y;
}Pos;


typedef struct Maze
{
int _mz[N][N];
Pos _entry;
}Maze;


void MazeInit(Maze* m,int arr[][N]);


void MazeGetPath(Maze* m);  //尋找路徑


void MazeGetShortPath(Maze* m); //最優路徑


void MazePrint(Maze* m);    //列印迷宮


int MazeEntry(Pos entry); //判斷迷宮入口是否正確

迷宮的實現:MazeFunction.c

#include"Maze.h"
#include"Stack.h"
void MazeInit(Maze*m,int arr[][N])
{
int i = 0;
int j = 0;
assert(m);
for(; i < N; i++)
{
j = 0;
for(;j < N; j++)
{
m->_mz[i][j] = arr[i][j];
}
}




}


void MazeGetPath(Maze* m)
{
m->_entry._x = 5;
m->_entry._y = 2;

 SqStackInit(&s);
Push(&s,m->_entry);
while(s._top)
{
Pos cur,next;
cur = StackTop(&s);
if(s._top != NULL)


m->_mz[cur._x][cur._y] = 2;


 if ((cur._x == 0 || cur._x == N - 1 || \
             cur._y == 0 || cur._y == N - 1) && (cur._x != m->_entry._x && cur._y != m->_entry._y))
 {
printf("找到出口\n");
return;
 }
//左
next = cur;
next._x -= 1;


if(CheckIsAccess(m,next) == 1)
{
Push(&s,next);
continue;
}


next = cur;
         //上
         next._y -= 1;
         if (CheckIsAccess(m, next) == 1)
         {
             Push(&s, next);
             continue;
         }


         next = cur;
         //右
         next._x += 1;
         if (CheckIsAccess(m, next) == 1)
         {
             Push(&s, next);
             continue;
         }


         next = cur;
         //下
         next._y += 1;
         if (CheckIsAccess(m, next) == 1)
         {
             Push(&s, next);
             continue;
         }




//回溯
         Pop(&s);
}

printf("迷宮出不去");
}
//判斷迷宮入口是否正確
int MazeEntry(Pos entry)
{
if(entry._x == 0||entry._x == N -1 ||entry._y == 0||entry._y == N -1 )
return 1;
return 0;


}


int CheckIsAccess(Maze* m,Pos next)
{
assert(m);
if(next._x >= 0 && next._x <N && next._y >=0 && next._y < N && m->_mz[next._x][next._y] == 1)


return 1;
return 0;
}


void MazePrint(Maze* m)
{
int i = 0;
int j = 0;
assert(m);
for (; i<N; ++i)
     {    
         for (j = 0; j<N; ++j)
         {
             printf("%d ", m->_mz[i][j]);
         }
         printf("\n");
     }
 }

迷宮的測試:Maze.c

#include"Maze.h"
int main()
{
Maze m;
int a[N][N] = {

{0,0,0,0,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0},
{0,0,1,1,1,0},
{0,0,1,1,1,1},
{0,0,1,0,0,0},
};
MazeInit(&m,a);
MazePrint(&m);
MazeGetPath(&m);
MazePrint(&m);
system("pause");
return 0;
}