测试环境:vs2013
Maze.h
#pragma once
#define MAX_ROW 6
#define MAX_COL 6
typedef struct Position
{
int _x;
int _y;
}Position;
typedef struct Maze
{
int _map[MAX_ROW][MAX_COL];
}Maze, *PMaze;
void InitMaze(PMaze pm, int map [][MAX_COL]);
int IsValidEntry(PMaze pm, Position entry);
int IsPass(PMaze pm, Position cur);
int IsExit(PMaze pm, Position cur, Position entry);
void PassMaze(PMaze pm, Position entry, PStack ps);
void PrintMaze(PMaze m, int map [][MAX_COL]);
Stack.h
#pragma once
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern struct Position;
typedef struct Position DataType;
typedef struct Stack
{
DataType *arr;
int capacity;
int size;
}Stack,* PStack;
void StackInit(PStack ps, int capacity);
void StackPush(PStack ps, DataType data);
void StackPop(PStack ps);
DataType StackTop(PStack ps);
int StackSize(PStack ps);
int StackEmpty(PStack ps);
Maze.c
#include "stack.h"
#include "Maze.h"
void PrintMaze(PMaze pm)
{
int i = 0 ;
assert (pm);
for (; i < MAX_ROW; ++i)
{
int j = 0 ;
for (; j < MAX_COL; ++j)
{
printf("%d " , pm->_map[i][j]);
}
printf("\n" );
}
}
void InitMaze(PMaze pm, int map[][MAX_COL])
{
int i = 0 ;
assert (pm);
for (; i < MAX_ROW; ++i)
{
int j = 0 ;
for (; j < MAX_COL; ++j)
{
pm->_map[i][j] = map[i][j];
}
}
}
int IsValidEntry(PMaze pm, Position entry)
{
assert (pm);
if ((entry._x == 0 || entry._y == 0 || entry._x == MAX_ROW - 1
|| entry._y == MAX_COL - 1 ) && (pm->_map[entry._x][entry._y] == 1 ))
return 1 ;
return 0 ;
}
int IsPass(PMaze pm, Position cur)
{
assert (pm);
if ((cur._x >= 0 && cur._x <= MAX_ROW-1 ) &&
(cur._y >= 0 && cur._y <= MAX_COL-1 ) &&
pm->_map[cur._x][cur._y] == 1 )
return 1 ;
return 0 ;
}
int IsExit(PMaze pm, Position cur, Position entry)
{
assert (pm);
if ((cur._x == 0 || cur._y == 0 || cur._x == MAX_ROW - 1
|| cur._y == MAX_COL - 1 ) && (pm->_map[cur._x][cur._y] == 1 )
&& ((cur._x != entry._x) || (cur._y != entry._y)))
return 1 ;
return 0 ;
}
void PassMaze(PMaze pm, Position entry, PStack ps)
{
Position cur;
Position next;
assert (pm);
assert (ps);
if (!IsValidEntry(pm, entry))
{
printf("迷宫入口不合法!!!\n" );
return ;
}
StackPush(ps, entry);
while (!StackEmpty(ps))
{
cur = StackTop(ps);
if (IsExit(pm, cur, entry))
{
pm->_map[cur._x][cur._y] = 2 ;
printf("已找到出口!!!\n" );
return ;
}
pm->_map[cur._x][cur._y] = 2 ;
next = cur;
next._x -= 1 ;
if (IsPass(pm, next))
{
StackPush(ps, next);
continue ;
}
next = cur;
next._y -= 1 ;
if (IsPass(pm, next))
{
StackPush(ps, next);
continue ;
}
next = cur;
next._y += 1 ;
if (IsPass(pm, next))
{
StackPush(ps, next);
continue ;
}
next = cur;
next._x += 1 ;
if (IsPass(pm, next))
{
StackPush(ps, next);
continue ;
}
pm->_map[cur._x][cur._y] = 3 ;
StackPop(ps);
}
printf("迷宫没有出口!!!\n" );
}
Stack.c
#include "stack.h"
#include "Maze.h"
void StackInit(PStack ps, int capacity)
{
if (NULL == ps)
return ;
ps->arr = (DataType *)malloc(sizeof (DataType)*capacity);
if (NULL == ps->arr)
{
printf("申请空间失败!!!\n" );
return ;
}
ps->capacity = capacity;
ps->size = 0 ;
}
void AddCapacity(PStack ps)
{
if (NULL == ps)
return ;
ps->arr = (DataType *)realloc(ps->arr, sizeof (DataType)*(ps->capacity) * 2 );
if (NULL == ps->arr)
{
printf("空间扩增失败!!!\n" );
return ;
}
ps->capacity = 2 * (ps->capacity);
}
void PrintStack(PStack ps)
{
int i = 0 ;
if (NULL == ps)
return ;
for (; i < ps->size; i++)
{
printf("%d " , ps->arr[i]);
}
printf("\n" );
}
void StackPush(PStack ps, DataType data)
{
if (NULL == ps)
return ;
if (ps->size == ps->capacity)
AddCapacity(ps);
ps->arr[ps->size] = data;
ps->size++;
}
void StackPop(PStack ps)
{
if (NULL == ps)
return ;
if (ps->size == 0 )
{
printf("栈已空,操作失败!!!\n" );
return ;
}
ps->size--;
}
DataType StackTop(PStack ps)
{
return ps->arr[ps->size - 1 ];
}
int StackSize(PStack ps)
{
if (NULL == ps)
{
printf("栈不存在!!!\n" );
return 0 ;
}
return ps->size;
}
int StackEmpty(PStack ps)
{
if (NULL == ps)
{
printf("栈不存在!!!\n" );
return 0 ;
}
if (ps->size)
return 0 ;
return 1 ;
}
test.c
#include "stack.h"
#include "Maze.h"
#include <windows.h>
void TestMaze(PStack ps)
{
int i = 0 ;
Position entry;
Maze m;
PMaze pm = &m;
StackInit(ps, 10 );
int map [6 ][6 ] = { { 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 , 0 , 1 , 1 },
{ 0 , 0 , 1 , 0 , 0 , 0 } };
InitMaze(pm, map );
PrintMaze(pm, map );
entry._x = 5 ;
entry._y = 2 ;
PassMaze(pm, entry, ps);
printf ("\n" );
PrintMaze(pm, map );
}
int main()
{
Stack s;
PStack ps = &s;
TestMaze(ps);
system("pause" );
return 0 ;
}