void MazeInit(Maze* maze)//初始化地图
{
if(maze == NULL) {
return;
}
int map[ROW][COL] = {
{0,1,0,0,0,0},
{0,1,1,1,0,0},
{0,1,0,1,1,0},
{1,1,0,0,1,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0}
};
size_t i = 0;
size_t j = 0;
for(; i < ROW; ++i) {
for(j = 0; j < COL; ++j) {
maze->map[i][j] = map[i][j];
}
}
return;
}
void MazeMapPrint(Maze* maze)//打印地图
{
if(maze == NULL) {
return;
}
printf("\n");
size_t i = 0;
size_t j = 0;
for(; i < ROW; ++i) {
for(j = 0; j < COL; ++j) {
printf("%2d ",maze->map[i][j]);
}
printf("\n");
}
}
int CanStay(Maze* maze, Point cur)//是否能够落脚
{
if(maze == NULL) {
return 0;
}
if(cur.col < 0 || cur.col >= COL || \
cur.row < 0 || cur.row >= ROW) {
return 0;
}
if(maze->map[cur.row][cur.col] == 1) {
return 1;
}
return 0;
}
void MarkStay(Maze* maze, Point cur)//标记落脚点
{
if(maze == NULL) {
return;
}
maze->map[cur.row][cur.col] = 2;
}
int isExit(Maze* maze, Point cur, Point entry)//判断是否是出口点
{
if(maze == NULL) {
return 0;
}
if((cur.row == 0 || cur.row == ROW - 1 || \
cur.col == 0 || cur.col == COL - 1) && \
(cur.col != entry.col || cur.row != entry.row)) {
return 1;
}
return 0;
}
int _GetPathMaze(Maze* maze, Point cur, Point entry)
{
printf("(%d,%d)\n",cur.row, cur.col);
if(maze == NULL) {
return 0;
}
//判断是否能落脚
if(!CanStay(maze, cur)) {
return 0;
}
//标记落脚点
MarkStay(maze, cur);
//判断是否是出口点
if(isExit(maze, cur, entry)) {
printf("找到一条路径\n");
return 1;
}
//如果不是出口点那么就判断它的四周
//预定顺序为上、右、下、左
Point up = cur;
up.row -= 1;
_GetPathMaze(maze, up, entry);
Point right = cur;
right.col += 1;
_GetPathMaze(maze, right, entry);
Point down = cur;
down.row += 1;
_GetPathMaze(maze, down, entry);
Point left = cur;
left.col -= 1;
_GetPathMaze(maze, left, entry);
return 1;
}
void GetPathMaze(Maze* maze,Point entry)//查找路径
{
if(maze == NULL) {
return;
}
entry.row = 0;
entry.col = 1;
_GetPathMaze(maze, entry, entry);
}
迷宫
最新推荐文章于 2022-12-21 20:56:45 发布