数据结构队列迷宫求解

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct {
	int x;
	int y;
	int pre	;
	int pre1;
} zuobiao;
typedef struct {
	zuobiao base[MAXSIZE];
	int front;
	int rear;
} sqqueue;
static 	int maze[8][10]= {{2,2,2,2,2,2,2,2,2,2},
	{2,0,1,1,1,0,1,1,1,2},
	{2,1,0,1,0,1,0,1,1,2},
	{2,0,1,0,0,1,1,1,1,2},
	{2,0,1,1,1,0,0,1,1,2},
	{2,1,0,0,1,1,1,0,0,2},
	{2,0,1,1,0,0,1,1,0,2},
	{2,2,2,2,2,2,2,2,2,2}
};
zuobiao start= {1,1,0,1} ;
//创造一个空循环队列
void InitQueue(sqqueue &q) {
	q.front=q.rear=0;
}
//插入元素e为q的新的队尾元素
int enqueue (sqqueue &q,zuobiao e) {
	if((q.rear+1)%MAXSIZE==q.front)
		return 0;//表示队列满
	q.base[q.rear]=e;
	q.rear=(q.rear+1)%MAXSIZE;
	return 1;
}

//下一个的地址
zuobiao next(zuobiao a,int direction) {

	switch (direction) {
		case 1:
			a.x++;
			break;//向南走
		case 2:
			a.y++;
			break;//向东走
		case 3:
			a.x--;
			break;//向北走
		case 4:
			a.y--;
			break;//向西走
		case 5:
			a.x++;
			a.y++;
			break;//向东南走
		case 6:
			a.x++;
			a.y--;
			break;//向西南走
		case 7:
			a.x--;
			a.y++;
			break;//向东北走
		case 8:
			a.x--;
			a.y--;
			break;//向西北走
	}
	return a;
}
//寻找路径
int find() {

	int k=0;
	int direction=1;
	zuobiao r;
	r=start;
	sqqueue q;
	InitQueue(q);
	enqueue (q,r);
	maze[r.x][r.y]=-1;
	while(k==0) { //k=0一直循环向下找
		while(direction<=8) {
			r = q.base[q.front];
			int l=r.pre1;
			r = next(r,direction);
			if (maze[r.x][r.y]==0 && r.x>=0 && r.y>=0) { //
				r.pre=l;
				r.pre1=l;
				enqueue(q,r);
				maze[r.x][r.y]=-1;
			}
			if (r.x==6 && r.y==8) { //循环终止条件
				k=1;
				break;
			}

			direction++;

		}
		if (k==0) {
			int v  = q.base[q.front].pre1;
			q.front=(q.front+1)%MAXSIZE;
			q.base[q.front].pre1=v+1;
			direction=1;
		}

	}
	zuobiao a[100];
	int p;
	a[0]=r;
	p = r.pre;
	int i=1;
	while(r.pre>0) {
		if(q.base[q.front].pre1 == p) {
			r =q.base[q.front];
			a[i]=r;
			i++;
			p=q.base[q.front].pre;
		}

		q.front--;

	}
	for (i=7; i>=0; i--) {
		printf("(%d,%d)-",a[i].x,a[i].y);
	}
	if(k==1)
		return 1;//表示有通路
	return 0;//表示没通路
}

int main() {
	int i,j;
	for(i=0; i<=7; i++) {
		for(j=0; j<=9; j++) {
			printf(" %d ",maze[i][j]);
		}
		printf("\n");
	}//打印迷宫
	find();
	printf("\n");

	return 0;
}

这个程序中的pre和pre1的含义和作用是什么

pre是zuobiao结构体里的一个属性,代表当前坐标的前驱坐标,pre和pre1应该分别代表横竖坐标

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632