#include <stdio.h>
#include <stdlib.h>
#define MaxRow 10
#define MaxCol 10
#define MAXSTACKSIZE 100
#define STACKINCREMENT 10
#define NOENOUGHROOM 10
#define STACKOVERFLOW 11
#define up 1
#define down 2
#define left 3
#define right 4
typedef struct {
int *abscissa;
int *ordinate;
int *direction;
int maxstacksize;
}SqStack;
SqStack Stack;
int top = -1; //初始化栈顶
//依次上下左右行走
int arrowx[4] = {0, 0, -1, 1};
int arrowy[4] = {-1, 1, 0, 0};
int Maze[MaxRow][MaxCol] = {}; //用来标记走过的路
int pathMaze[MaxRow][MaxCol] = {}; //用来存储正确路径
int maxabs = 1, maxord = 1; //用户输入的迷宫大小
void InitStack() {
int *absbase, *ordbase, *direbase;
absbase = (int *)malloc(MAXSTACKSIZE*sizeof(int));
ordbase = (int *)malloc(MAXSTACKSIZE*sizeof(int));
direbase = (int *)malloc(MAXSTACKSIZE*sizeof(int));
if(!absbase || !ordbase || !direbase)
exit(NOENOUGHROOM);
Stack.abscissa = absbase;
Stack.ordinate = ordbase;
Stack.direction = direbase;
Stack.maxstacksize = MAXSTACKSIZE;
}
void push(int abscissa, int ordinate, int direction) {
++top;
//判断是否溢出
if(top >= Stack.maxstacksize){
int *newabsbase, *newordbase, *newdirebase;
newabsbase = (int *)realloc(Stack.abscissa, (MAXSTACKSIZE + STACKINCREMENT)*sizeof(int));
newordbase = (int *)realloc(Stack.ordinate, (MAXSTACKSIZE + STACKINCREMENT)*sizeof(int));
newdirebase = (int *)realloc(Stack.direction, (MAXSTACKSIZE +STACKINCREMENT)*sizeof(int));
if(!newordbase || !newabsbase || !newdirebase)
exit(NOENOUGHROOM);
Stack.abscissa = newabsbase;
Stack.ordinate = newordbase;
Stack.direction = newdirebase;
}
*(Stack.abscissa + top) = abscissa;
*(Stack.ordinate + top) = ordinate;
*(Stack.direction + top -1) = direction;
}
void pop() {
//判断是否溢出
if(top == -2)
exit(STACKOVERFLOW);
*(Stack.abscissa + top) = 0;
*(Stack.ordinate + top) = 0;
*(Stack.direction + top) = 0;
--top;
}
//打印点和方向
void printPointAndDirection(int n) {
switch (*(Stack.direction + n)) {
case up:
printf("(%d, %d, up)\n", *(Stack.abscissa + n), *(Stack.ordinate + n));
break;
case down:
printf("(%d, %d, down)\n", *(Stack.abscissa + n), *(Stack.ordinate + n));
break;
case left:
printf("(%d, %d, left)\n", *(Stack.abscissa + n), *(Stack.ordinate + n));
break;
case right:
printf("(%d, %d, right)\n", *(Stack.abscissa + n), *(Stack.ordinate + n));
break;
}
}
//打印迷宫解
void printPath() {
printf("输出路线:\n");
int i = 0;
while(1) {
printPointAndDirection(i);
if(*(Stack.abscissa + i) == maxabs && *(Stack.ordinate + i) == maxord)
break;
++i;
}
}
//标记走过的路
void markMaze(int abscissa, int ordinate) {
Maze[ordinate][abscissa] = 2;
pathMaze[ordinate][abscissa] = 2;
}
//打印迷宫并标出正确出路
void printPathMaze() {
printf("\n迷宫输出是:\n");
for(int i = 1;i <= maxord;i++) {
for(int j = 1;j <= maxabs;j++) {
printf("%d ", pathMaze[i][j]);
}
printf("\n");
}
printf("\n\n");
}
//打印用来做标记的迷宫
void printMaze() {
printf("\n迷宫输出是:\n");
for(int i = 1;i <= maxord;i++) {
for(int j = 1;j <= maxabs;j++) {
printf("%d ", Maze[i][j]);
}
printf("\n");
}
printf("\n\n");
}
//初始化迷宫
void InitMaze() {
printf("\n请输入迷宫的长和宽:\n");
scanf("%d %d", &maxabs, &maxord);
printf("请输入迷宫:\n");
for(int i = 1;i <= maxord;i++) {
for(int j = 1;j <= maxabs;j++) {
scanf("%d", &Maze[i][j]);
pathMaze[i][j] = Maze[i][j];
}
}
}
//寻找出路
void runMaze() {
//判断第一个方向
push(1, 1 , 0);
markMaze(1, 1);
int flag = 0;
while(1) {
int xNow, yNow;
int i = 0;
xNow = *(Stack.abscissa + top);
yNow = *(Stack.ordinate + top);
if(xNow == maxabs && yNow == maxord) {
flag = 1;
break;
}else if(top == -2)
break;
for(i = 0;i < 4;i++) {
int xNext, yNext;
xNext = xNow + arrowx[i];
yNext = yNow + arrowy[i];
//找到可走的点
if(xNext > 0 && xNext <= maxabs && yNext > 0 && yNext <= maxord && Maze[yNext][xNext] == 0) {
push(xNext, yNext, i+1);
markMaze(xNext, yNext);
break;
}
}
//无路可走
if(i == 4) {
pathMaze[yNow][xNow] = 0;
pop();
}
}
if(flag) {
printPath();
printPathMaze();
} else {
printf("\n没有出路,无解!\n");
}
}
int main() {
InitMaze();
runMaze();
}
用栈实现迷宫求解(改进版,C语言)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- ~~~ /** * 1 从maze.txt读入迷宫 * 2 从入口坐标(1,1)寻找到出口的通路 * 3 显示迷宫...
- /** * 用不带头结点的单链表构造的链式栈 * LinkedStack * 创建人:guxiaohao * 时间...