#ifndef LINKSTACK_H
#define LINKSTACK_H
typedef enum { true = 1, false = 0 } bool;
typedef int Data;
struct Node {
Data data;
struct Node* next;
};
typedef struct Node* PNode;
typedef struct Node* LinkStack;
LinkStack CreatStack()
{
LinkStack top = (LinkStack)malloc(sizeof(struct Node));
if (top != NULL) top->next = NULL;
else printf("Fail!");
return top;
}
void Push(LinkStack top, Data x) {
PNode p = (PNode)malloc(sizeof(struct Node));
if (p == NULL) printf("Fail!");
else {
p->data = x;
p->next = top->next;
top->next = p;
}
}
void Pop(LinkStack top) {
PNode p;
if (top->next == NULL) printf("Empty!");
else {
p = top->next;
top->next = p->next;
free(p);
}
}
Data Top(LinkStack top) {
if (top->next == NULL) printf("Empty!");
else return top->next->data;
}
bool Empty(LinkStack top) {
return top->next == NULL;
}
#endif
#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')
{
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
#include <stdlib.h>
#include <stdio.h>
#include "LinkStack.h"
#include "Maze.h"
int MazeDFS(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 X = NULL;
LinkStack Y = NULL;
int posx, posy;
int prex, prey;
int** mark;
int j, i;
int mov;
mark = (int**)malloc(sizeof(int*) * maze->size);
for (i = 0; i < maze->size; i++)
{
mark[i] = (int*)malloc(sizeof(int) * maze->size);
for (j = 0; j < maze->size; j++)
mark[i][j] = maze->data[i][j];
}
X = CreatStack();
Y = CreatStack();
mark[x1][y1] = 2;
Push(X, x1);
Push(Y, y1);
while (!Empty(X))
{
prex = Top(X);
prey = Top(Y);
Pop(X);
Pop(Y);
mov = 0;
while (mov < 8)
{
posx = prex + direction[mov][0];
posy = prey + direction[mov][1];
int px = 0;
int py = 0;
if (maze->data[posx][posy] == 0 && mark[posx][posy] == 0)
{
mark[posx][posy] = 2;
Push(X, prex);
Push(Y, prey);
prex = posx;
prey = posy;
mov = 0;
}
else mov++;
if (posx == x2 && posy == y2)
{
Push(X, prex);
Push(Y, prey);
printf("已到达,路径如下:\n");
while (!Empty(X)) {
printf("(%d,%d) <- ", Top(X), Top(Y));
Pop(X);
Pop(Y);
}
printf("(起点)\n");
for (int i = 0; i < maze->size; i++)
{
for (int j = 0; j < maze->size; j++)
{
if (mark[i][j] == 1)
printf("%c ", 1);
else if (mark[i][j] == 2)
printf(" *");
else
printf(" ");
}
printf("\n");
}
return 1;
}
}
}
return 0;
}
int main()
{
Maze* maze = InitMaze(9);
ReadFile(maze,"./maze.txt");
MazeDFS(1,1,9,8,maze);
return 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