#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<conio.h>
#define ARRAY_SIZE 10
#define FOUND_WAYS 20
enum
{
CELL_TYPE_ROAD = 0, //可以走的路
CELL_TYPE_WALL, //迷宫四周的及迷宫内部路两测的墙,不能走
CELL_TYPE_ROAD_WALKING, //当前正在走的路对应的栈
CELL_TYPE_ROAD_WALKED //已走过的路记录下来,防止重复走
};
//设置迷宫,最外围1为墙 里边0为可走路径 ,1为障碍
int MazeMap[ARRAY_SIZE][ARRAY_SIZE] =
{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 1, 1},
{1, 0, 1, 1, 1, 1, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 1, 1},
{1, 0, 1, 1, 0, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
struct
{
int x, y, Direction;
}WalkSteps[FOUND_WAYS][100]; //x, y分别为垂直和水平方向
int FoundWays = 0; //当前找到多少条路
int top = 0;
void display(int *pMazeMap, int currentX, int currentY, int Direction)
{
COORD pos = {0};
int i, j;
int num;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
putchar('\n');
for(i = 0; i < ARRAY_SIZE; i++)
{
for(j = 0;j < ARRAY_SIZE; j++)
{
int CellType = *(pMazeMap + i * ARRAY_SIZE + j);
if(CELL_TYPE_ROAD == CellType)
printf(" ");
else if(CELL_TYPE_WALL == CellType)
printf(" * ");
else if(CELL_TYPE_ROAD_WALKED == CellType)
printf(" 0 ");
else if(CELL_TYPE_ROAD_WALKING == CellType)
printf(" 3 ");
}
putchar('\n');
}
putchar('\n');
printf("Current: (%1d,%1d) Direction:%1d", currentX, currentY, Direction);
for(int Ways = 0; Ways < FoundWays; Ways++)
{
printf("迷宫路径如下 FoundWays:%d\n", FoundWays + 1);
printf("start->");
for(int index = 0; WalkSteps[Ways][index].x != 0; index++)
{
WalkSteps[Ways][index].x = WalkSteps[Ways][index].x;
WalkSteps[Ways][index].y = WalkSteps[Ways][index].y;
WalkSteps[Ways][index].Direction = WalkSteps[Ways][index].Direction;
printf("(%d,%d)-> ", WalkSteps[Ways][index].x, WalkSteps[Ways][index].y);//把找到的路径输出
num++;
if(num % 8 == 0)
printf("\n");
}
printf("end!\n");
printf("\n\n");
}
Sleep(500);
}
void start()
{
//栈
int x, y, Direction, FoundFlag;//Direction为设置方向,上下左右。find为设置找不找得到路
WalkSteps[FoundWays][top].x = 1;
WalkSteps[FoundWays][top].y = 1;
MazeMap[1][1] = - 1; //初始位置
FoundFlag = 0; //未找到路为0
Direction = - 1;
while(top>-1)
{
//找到出口
if(WalkSteps[FoundWays][top].x == 8 && WalkSteps[FoundWays][top].y == 8)
{
for(x = 0; x <= top; x++)
{
WalkSteps[FoundWays + 1][x] = WalkSteps[FoundWays][x];
}
FoundWays++;
}
while(Direction < 4 && FoundFlag == 0)
{
Direction++;
switch(Direction)
{
case 0:x = WalkSteps[FoundWays][top].x - 1; y = WalkSteps[FoundWays][top].y; break;//方向为上
case 1:x = WalkSteps[FoundWays][top].x; y = WalkSteps[FoundWays][top].y + 1; break;//方向为右
case 2:x = WalkSteps[FoundWays][top].x + 1; y = WalkSteps[FoundWays][top].y; break;//方向为下
case 3:x = WalkSteps[FoundWays][top].x; y = WalkSteps[FoundWays][top].y - 1; break;//方向为左
}
if(MazeMap[x][y] == CELL_TYPE_ROAD)
FoundFlag = 1; //找到路为1
}
//能找到路
if(FoundFlag == 1)
{
//记录找到的路
WalkSteps[FoundWays][top].Direction = Direction;
top++;
WalkSteps[FoundWays][top].x = x;
WalkSteps[FoundWays][top].y = y;
MazeMap[x][y] = CELL_TYPE_ROAD_WALKING;
display((int *)MazeMap, x, y, Direction);
Direction = -1;
FoundFlag = 0; //重新调整方向
}
else //不能找到路
{
int x = WalkSteps[FoundWays][top].x;
int y = WalkSteps[FoundWays][top].y;
MazeMap[x][y] = CELL_TYPE_ROAD_WALKED;
display((int *)MazeMap, x, y, Direction);
top--;
Direction = WalkSteps[FoundWays][top].Direction; //找不到的话退栈
}
}
}
int main()
{
start();
}