-
运行结果截图(八方向可走)
-
手写栈 LinkStack.h
-
图 Maze.h
#pragma warning(disable:4996)
#ifndef MAZE_H_
#define MAZE_H_
#include<conio.h>
#include <stdlib.h>
#include <stdio.h>
#include "LinkStack.h"
typedef struct MAZE
{
int size;
int** data;
}Maze;
void PrintMaze(Maze* maze);
Maze* InitMaze(int size)
{
int i;
Maze* maze = (Maze*)malloc(sizeof(Maze));
maze->size = size + 2;
maze->data = (int**)malloc(sizeof(int*) * maze->size);
for (i = 0; i < maze->size; i++)
{
maze->data[i] = (int*)malloc(sizeof(int) * maze->size);
}
for (i = 0; i < maze->size; i++)
{
maze->data[i][0] = maze->data[0][i] = 1;
maze->data[size+1][i] = maze->data[i][size+1] = 1;
}
return maze;
}
void Read(Maze* maze)
{
int i, j;
printf("输入迷宫结构:\n");
for (i = 1; i < maze->size - 1; i++)
{
for (j = 1; j < maze->size - 1; j++)
{
scanf_s("%d", &maze->data[i][j]);
}
}
}
void ReadFile(Maze* maze,const char* file)
{
FILE* fp = NULL;
char buff[255];
fp = fopen(file, "r");
int i = 1, j = 1;
int n = maze->size;
char ch = fgetc(fp);
while (ch != EOF) {
if (ch == '1' || ch == '0')
{
//putchar(ch);
maze->data[i][j] = ch - 48;
j++;
if (j == n-1)
{
j = 1;
i++;
}
if (i == n - 1) break;
}
ch = fgetc(fp);
}
fclose(fp);
printf("读取文件%s获取迷宫(有边框)如下: \n",file);
PrintMaze(maze);
}
void PrintMaze(Maze* maze)//打印迷宫
{
for (int i = 0; i < maze->size; i++)
{
for (int j = 0; j < maze->size; j++)
{
if (maze->data[i][j] == 1)
printf("%c ", 1);//方块
else if (maze->data[i][j] == '*')
printf(" *");
else
printf(" ");
}
printf("\n");
}
printf("\n");
}
#endif // !MAZE_H_
- 主程序
#include <stdlib.h>
#include <stdio.h>
#include "LinkStack.h"
#include "Maze.h"
int MazeDFSAll(int x1, int y1, int x2, int y2, Maze* maze)
{
int direction[8][2] = { {0,1}, {1,1}, {1,0}, {1,-1},{0,-1}, {-1,-1}, {-1,0}, {-1,1} };
LinkStack stack = CreatStack();
Point* point = (Point*)malloc(sizeof(struct Point));
point->x = x1;
point->y = y1;
point->dis = -1;
Push(stack, point);
maze->data[x1][y1] = '*';//将入口的迷宫值置为*,避免重复走到该方块
int count = 1;
bool _find = false;
int nx=0, ny=0;
bool flag = false;
while (!Empty(stack))
{
Point* curPoint = Top(stack);
int curX = curPoint->x;
int curY = curPoint->y;
int curDis = curPoint->dis;
if (curX == x2 && curY == y2)//找到了出口,输出该路径
{
flag = true;
printf("迷宫路径%d为:\n", count++);
PrintMaze(maze);
printf("\n");
Pop(stack);
maze->data[curX][curY] = 0;
free(curPoint);
}
else {
_find = false;
while (curDis < 8 && !_find)
{
curDis++;
nx = curX + direction[curDis][0];
ny = curY + direction[curDis][1];
if (curDis < 8 && maze->data[nx][ny] == 0) _find = true;
}
if (_find)
{
//curPoint->dis = curDis; //?
Top(stack)->dis = curDis;
Point* temp = (Point*)malloc(sizeof(struct Point));
temp->x = nx;
temp->y = ny;
temp->dis = -1;
Push(stack, temp);
maze->data[nx][ny] = '*';
//PrintMaze(maze);
}
else
{
curPoint = Top(stack);
Pop(stack);
maze->data[curPoint->x][curPoint->y] = 0;
free(curPoint);
//PrintMaze(maze);
}
}
}
if (flag == 1)
{
printf("!!");
return true;
}
return false;
}
int main()
{
Maze* maze = InitMaze(4);
ReadFile(maze, "./maze1.txt");
MazeDFSAll(1, 1, 4, 4, maze);
//Maze* maze = InitMaze(9);
//ReadFile(maze, "./maze.txt");
//MazeDFSAll(1, 1, 9, 8, maze);
return 0;
}
- 测试用例 maze.txt
0 1 0 0
1 0 0 1
0 1 0 0
1 0 1 0
0 0 0 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1
0 0 0 1 0 0 1 1 1
1 1 0 1 1 1 0 1 1
1 1 0 1 1 1 1 0 1
1 1 0 0 0 0 0 1 1
1 1 0 1 0 1 0 1 1
1 1 0 0 1 0 1 0 1
1 1 1 0 1 0 0 0 1