stack.h
#pragma once
#include <malloc.h>
#include <assert.h>
#include<stdio.h>
#include <stdlib.h>
typedef struct Pos
{
int _row;
int _col;
}Pos;
typedef Pos STDataType;
#define DEFAULT_SZ 5
typedef struct Node
{
STDataType * data;
struct Node* next;
}NODE, *PNODE;
typedef struct Stack
{
STDataType * data;
PNODE _top; // 栈顶
PNODE _end; //栈底
int _capacity; // //栈的总大小
int _size;//当前栈的大小
}Stack;
void StackInit(Stack* ps);
void StackDestroy(Stack* ps);
void StackPush(Stack* ps, STDataType x);
void StackPop(Stack* ps);
STDataType StackTop(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
void StackPrint(Stack *ps);
void TestStack();
maze.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"Stack.h"
#define N 6
static int maze[N][N] =
{
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 1, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0 },
};
int GetMazePath(Pos entry, Pos exit);
void MazePrint();
int GetMazePathR(Pos entry,Pos exit);
int CheckAccess(Pos next);
int pathsize = 0;
void MazeGetShortPath(Pos entry, Pos exit);
stack.c
#include"Stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->data = (STDataType *)malloc(sizeof(STDataType)* DEFAULT_SZ);
ps->_capacity = DEFAULT_SZ;
ps->_top = ps->_end;
ps->_size = 0;
}
void StackDestroy(Stack* ps)//销毁栈
{
assert(ps);
int i, len;
len = ps->_size;
if (ps->_top == ps->_end)
{
printf("栈为空,无法销毁\n");
return;
}
else
{
for (i = 0; i < len; i++)
{
free(ps->_end);
ps->_end++;
}
ps->_size = 0;
ps->_capacity = 0;
ps->_top = ps->_end = NULL;
}
}
void StackPush(Stack* ps, STDataType x)
{
PNODE newNode = (PNODE)malloc(sizeof(NODE));
newNode->data = x;
newNode->next = ps->_top;
ps->_top = newNode;
}
void StackPop(Stack* ps)
{
PNODE tmp = (PNODE)malloc(sizeof(NODE));
assert(ps != NULL&&ps->_top != NULL&&ps->_end != NULL);
tmp = ps->_top->next;
free(ps->_top);
ps->_top = NULL;
ps->_top = tmp;
ps->_size--;
}
STDataType StackTop(Stack* ps)
{
assert(ps != NULL);
STDataType d = ps->_top->data;
return d;
}
int StackEmpty(Stack* ps)
{
if (ps->_top = ps->_end)
{
return 1;
}
else
{
return 0;
}
}
int StackSize(Stack* ps)
{
assert(ps != NULL);
while (ps)
{
ps->_top++;
ps->_size++;
}
return ps->_size;
}
void StackPrint(Stack *ps)
{
PNODE tmp = ps->_top;
while (tmp != ps->_end)
{
printf("%d", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
maze.c
#include"maze.h"
#include"Stack.h"
void MazePrint()
{
size_t i, j;
for (i = 0; i < N; ++i)
{
for (j = 0; i < N; ++j)
{
printf("%d", maze[i][j]);
}
printf("\n");
}
printf("\n");
}
int GetMazePath(Pos entry, Pos exit)
{
Stack path;
StackInit(&path);
StackPush(&path, entry);
while (StackEmpty(&path))
{
Pos cur = StackTop(&path);
Pos next;
maze[cur._row][cur._col] = 2;
//if (cur._row == exit._row&&
//cur._col == exit._col)
if (cur._col==5)
{
StackDestroy(&path);
return 1;
}
//探测下一个可以去的位置
//上
next = cur;
next._row -= 1;
if (CheckAccess(next))
{
StackPush(&path, next);
continue;
}
//下
next = cur;
next._row += 1;
if (CheckAccess(next))
{
StackPush(&path, next);
continue;
}
//左
next = cur;
next._col -= 1;
if (CheckAccess(next))
{
StackPush(&path, next);
continue;
}
//右
next = cur;
next._col += 1;
if (CheckAccess(next))
{
StackPush(&path, next);
continue;
}
StackPop(&path);
}
return 0;
}
int GetMazePathR(Pos entry, Pos exit)
{
Stack path;
StackInit(&path);
StackPush(&path, entry);
while (StackEmpty(&path))
{
Pos cur = StackTop(&path);
Pos next;
maze[cur._row][cur._col] = 2;
if (cur._row == exit._row&&
cur._col == exit._col)
{
StackDestroy(&path);
return 1;
}
//探测下一个可以去的位置
//上
next = cur;
next._row -= 1;
if (CheckAccess(next))
{
StackPush(&path, next);
continue;
}
//下
next = cur;
next._row += 1;
if (CheckAccess(next))
{
StackPush(&path, next);
continue;
}
//左
next = cur;
next._col -= 1;
if (CheckAccess(next))
{
StackPush(&path, next);
continue;
}
//右
next = cur;
next._col += 1;
if (CheckAccess(next))
{
StackPush(&path, next);
continue;
}
StackPop(&path);
}
return 0;
}
int CheckAccess(Pos next)
{
if (next._row >= 0 && next._row<N
&&next._col >= 0 && next._col>N
&&maze[next._row][next._col]==1)
{
return 1;
}
else
{
return 0;
}
}
int CheckAccessShort(Pos cur,Pos next)
{
if (next._row >= 0 && next._row<N
&&next._col >= 0 && next._col>N
&&(maze[next._row][next._col] == 1||maze[next._row][next._col]>maze[cur._row][cur._col]+1))
{
return 1;
}
else
{
return 0;
}
}
void MazeGetShortPath(Pos entry, Pos exit)
{
Stack path;
StackInit(&path);
StackPush(&path, entry);
maze[entry._row][exit._col];
while (StackEmpty(&path))
{
Pos cur = StackTop(&path);
Pos next;
//maze[cur._row][cur._col] = 2;
if (cur._col==5)
{
if (pathsize==0||
StackSize(&path) < pathsize)
{
pathsize = StackSize(&path);
}
}
//探测下一个可以去的位置
//上
next = cur;
next._row -= 1;
if (CheckAccessShort(cur,next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
StackPush(&path, next);
continue;
}
//下
next = cur;
next._row += 1;
if (CheckAccessShort(cur,next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
StackPush(&path, next);
continue;
}
//左
next = cur;
next._col -= 1;
if (CheckAccessShort(cur,next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
StackPush(&path, next);
continue;
}
//右
next = cur;
next._col += 1;
if (CheckAccessShort(cur,next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
StackPush(&path, next);
continue;
}
StackPop(&path);
}
//return 0;
}
test.c
#include"maze.h"
#include"Stack.h"
void TestMaze()
{
Pos entry,exit;
entry._row = 5;
entry._col = 2;
exit._row = 4;
exit._col = 5;
MazePrint();
printf("是否有出口:%d\n", GetMazePath(entry, exit));
printf("最短路径:%d\n", pathsize);
MazePrint();
}
int main()
{
TestMaze();
system("pause");
return 0;
}