stack.h
#ifndef _STACK_H__
#define _STACK_H__
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
enum DIRE{R,D,L,U,N};
typedef struct Pos{
int x; //行
int y; //列
enum DIRE dire;//方向
}Pos;
//typedef Pos T;
typedef int T;
typedef struct Stack{
T *base;
size_t capcity;
size_t size;
}Stack;
int stack_init(Stack *s,size_t cap); //初始化堆栈
bool stack_is_empty(Stack *s); //判断堆栈是否为空
bool stack_is_full(Stack *s); //判断堆栈是否满
T stack_top(Stack *s); //查看堆栈的栈顶元素
T stack_pop(Stack *s); //从堆栈中弹出一个元素
void stack_push(Stack *s,T data); //往堆栈中压入一个元素
void stack_destroy(Stack *s); //销毁堆栈
void stack_clear(Stack *s); //清空堆栈
void stack_foreach(Stack *s,void (*foreach)(T)); //遍历堆栈中的元素
#endif //_STACK_H__
stack.c
#include "stack.h"
int stack_init(Stack *s,size_t cap){
s->base = calloc(cap,sizeof(T));
if(s->base==NULL){
return -1;
}
s->capcity = cap;
s->size = 0;
return 0;
}
bool stack_is_empty(Stack *s){
return s->size == 0;
}
bool stack_is_full(Stack *s){
return s->size == s->capcity;
}
T stack_top(Stack *s){
return s->base[s->size-1];
}
T stack_pop(Stack *s){
return s->base[--s->size];
}
void stack_push(Stack *s,T data){
s->base[s->size++] = data;
}
void stack_destroy(Stack *s){
free(s->base);
s->base = NULL;
}
void stack_clear(Stack *s){
s->size = 0;
}
void stack_foreach(Stack *s,void (*foreach)(T)){
int i;
for(i=0;i<s->size;i++){
foreach(s->base[i]);
}
}
maze.c
#include "stack.h"
int maze[9][11]={ //0表示路 1表示墙
{0,0,0,1,0,1,1,0,1,1,1},
{0,1,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0,1,0,0},
{0,0,0,0,1,0,1,0,1,1,0},
{0,1,0,1,0,0,0,1,0,0,0},
{0,1,0,1,0,1,1,1,1,0,1},
{1,1,0,0,0,1,0,0,0,0,0},
{0,0,1,0,1,1,0,1,0,1,1},
{1,1,0,0,0,0,0,1,0,0,0},
};
int sx = 0,sy = 0;
int tx = 8,ty = 10;
//返回-1表示迷宫走不出来 0能走出来
int walkmaze(){
Stack s;
stack_init(&s,100);
Pos pos = {sx,sy,N};
stack_push(&s,pos);
maze[0][0] = -1;
int nx = 0,ny = 0;//已经走过的最后一个点
enum DIRE dire = R;//记录探索的方向
while(1){
pos = stack_top(&s);
if(pos.x == tx && pos.y == ty){
return 0;
}
switch(dire){
case R:
if(ny+1<11 && maze[nx][ny+1] == 0){
pos.x = nx;
pos.y = ++ny;
maze[nx][ny] = -1;
pos.dire = dire;
stack_push(&s,pos);
break;
}else{
++dire;
}
case D:
if(nx+1<9 && maze[nx+1][ny] == 0){
pos.x = ++nx;
pos.y = ny;
pos.dire = dire;
maze[nx][ny] = -1;
stack_push(&s,pos);
break;
}else{
++dire;
}
case L:
if(ny-1>=0 && maze[nx][ny-1] == 0){
pos.x = nx;
pos.y = --ny;
pos.dire = dire;
maze[nx][ny] = -1;
stack_push(&s,pos);
break;
}else{
++dire;
}
case U:
if(nx-1>=0 && maze[nx-1][ny] == 0){
pos.x = --nx;
pos.y = ny;
pos.dire = dire;
maze[nx][ny] = -1;
stack_push(&s,pos);
}else{
++dire;
}
}
if(dire == N){//此路不通
pos = stack_pop(&s);//走的这一步不可以 往前退
if(pos.dire == N){//起点位置无方向 N 起点都被回退了 说明无解
return -1;
}
dire = pos.dire+1;
maze[nx][ny] = -2;//死路
pos = stack_top(&s);//获得最后位置
nx = pos.x;
ny = pos.y;
}else{
dire = R;
}
}
stack_destroy(&s);
}
void showroad(){
int i,j;
for(i=0;i<9;i++){
for(j=0;j<11;j++){
if(maze[i][j]==1){
printf("#");
}else if(maze[i][j] == -1){
printf("O");
}else{
printf(" ");
}
}
printf("\n");
}
}
//int sx=0,sy=0;
//int tx=8,ty=10;
//保存路径
int main(){
int ret = walkmaze();
if(ret == 0){
showroad();
}else{
printf("此迷宫无解!\n");
}
return 0;
}