C语言[队列]实现迷宫求解(文件读取图)

  • 运行结果截图(八方向可走)

运行结果

  • 手写队列 Queue.h
#ifndef SEQQUEUE_H
#define SEQQUEUE_H
#include <stdio.h>
#include <stdlib.h>

typedef struct Point
{
	int x;
	int y;
	struct Point* pre;  // 保存路径中的上一个节点
}Point;

typedef Point* DataType;

struct Node
{
	DataType data;
	struct Node* next;
};
typedef struct Node* PNode;
struct Queue
{
	PNode first;
	PNode tail;
};
typedef struct Queue* LinkQueue;

LinkQueue CreatQueue()
{
	LinkQueue p;
	p = (LinkQueue)malloc(sizeof(struct Queue));
	if (p != NULL)
	{
		p->first = NULL;
		p->tail = NULL;
	}
	else printf("wrong!\n");
	return p;
}

int Empty(LinkQueue q)
{
	return(q->first == NULL);
}

void Push(LinkQueue q, DataType x)
{
	PNode p;
	p = (PNode)malloc(sizeof(struct Node));
	if (p == NULL) printf("wrong!\n");
	else {
		p->data = x;
		p->next = NULL;
		if (q->first == NULL)
		{
			q->first = p;
			q->tail = p;
		}
		else
		{
			q->tail->next = p;
			q->tail = p;
		}

	}

}
void Pop(LinkQueue q)
{
	PNode p;
	if (q->first == NULL)
		printf("empty!\n");
	else
	{
		p = q->first;
		q->first = q->first->next;
		free(p);
	}
}

DataType Front(LinkQueue q)
{
	if (q->first == NULL)
	{
		printf("empty!\n");
		exit(-1);
	}
	else return (q->first->data);
}

#endif // !SEQQUEUE_H
#include <stdlib.h>
#include <stdio.h>
#include "Queue.h"
#include "Maze.h"

void PrintPath(Point* p, Maze* maze)
{
	int mark[11][11] = { {0} };
	for (int i = 0; i < 11; i++)
	{
		for (int j = 0; j < 11; j++)
		{
			mark[i][j] = maze->data[i][j];
		}
	}
	while (p)
	{
		printf("(%d,%d) <- ", p->x, p->y);
		mark[p->x][p->y] = '*';
		p = p->pre;
	}

	printf("出发\n");
	for (int i = 0; i < 11; i++)
	{
		for (int j = 0; j < 11; j++)
		{
			if (mark[i][j] == 1)
				printf("%c ", 1);//方块
			else if (mark[i][j] == '*')
				printf(" *");
			else
				printf("  ");
		}
		printf("\n");
	}
}

void MazeBFS(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} };
	LinkQueue queue = CreatQueue(); //队列
	int** mark;  //标记走过的点
	int n = maze->size;
	int mov;
	mark = (int**)malloc(sizeof(int*) * n);
	for (int i = 0; i < maze->size; i++)
	{
		mark[i] = (int*)malloc(sizeof(int) * maze->size);
		for (int j = 0; j < maze->size; j++)
			mark[i][j] = maze->data[i][j];
	}

	Point* point = (Point*)malloc(sizeof(struct Point));
	point->x = x1;
	point->y = y1;
	point->pre = NULL;

	Push(queue, point);

	int path_index = 0;
	while (!Empty(queue))
	{
		Point* curPoint = Front(queue);
		Pop(queue);
		mark[curPoint->x][curPoint->y] = 2;

		mov = 0;

		while (mov < 8)
		{
			int nx = curPoint->x + direction[mov][0];
			int ny = curPoint->y + direction[mov][1];
			Point* nextPoint = (Point*)malloc(sizeof(struct Point));;
			nextPoint->x = nx;
			nextPoint->y = ny;

			if (nx > 0 && nx < n-1 && ny > 0 && ny < n-1 && mark[nx][ny] == 0)
			{
				//Point tmp = curPoint;

				nextPoint->pre = curPoint;
				mark[nx][ny] = 2;

				if (nx == x2 && ny == y2)
				{
					printf("已到达,路径如下:\n");
					PrintPath(nextPoint,maze);
					//return;
				}
				Push(queue,nextPoint);
			}
			else mov++;
		}
	}
}

int main()
{
	Maze* maze = InitMaze(9);
	ReadFile(maze, "./maze.txt");
	MazeBFS(1, 1, 9, 8, maze);
	return 0;
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值