队列走迷宫

迷宫问题。假设迷宫有m行n列构成,有一个入口和一个出口,入口坐标为(1,1),出口坐标为(m,n),式设计并验证一下算法:找出一条从入口通往出口的路径,或报告一个“无法通过”的信息。

实验代码:

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 65535 
typedef struct qstru{
		int x;
		int y;
		int pre;
}QElemType;


typedef int Status;


typedef struct queue{
        QElemType *base;
        int front; //指示队头位置
        int rear;  //指示队尾位置
}SqQueue;


void initQueue(SqQueue *q){
	q->base = (QElemType*)malloc(SIZE*sizeof(QElemType));
	q->front=q->rear=0;
}


void enQueue(SqQueue *q,QElemType e){
		q->base[q->rear]=e;
		q->rear++;
}


void deQueue(SqQueue *q,QElemType *e){
		*e=q->base[q->front];
		q->front++;
}


void printq(SqQueue q,int pos){//输出迷宫路径 
	if(q.base[pos].pre!=-1)
		printq(q,q.base[pos].pre);
	printf("( %d, %d)\n",q.base[pos].x,q.base[pos].y);
}


int **randomMap(int m,int n){//生成随机迷宫 
	srand((int)time(NULL));
	int **a,i,j;
	a=(int **)malloc(m*sizeof(int *));
	for( i=0;i<m;i++){
		a[i]=(int *)malloc(n*sizeof(int));
	}
	
	for( i=0;i<m;i++){
		for( j=0;j<n;j++){
		 	if(i==0||i==m-1||j==0||j==n-1)
				a[i][j]=1;
			else
				a[i][j]=rand()%2;
		}
		
	}
	a[1][1]=0,a[m-2][n-2]=0;
	return a; 
} 
void printMap(int **m,int a,int b){//输出迷宫地图 
	for(int i=0;i<a;i++){
		for(int j=0;j<b;j++){
			printf("%d ",m[i][j]);
		}
		printf("\n");
	}
}
int main(){
	SqQueue Q;
	int a,b,i,j;
	printf("输入迷宫的长和宽:");
	scanf("%d %d",&a,&b);
	int **m=randomMap(a+2,b+2);
	printMap(m,a+2,b+2);


	int delta[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}, {-1,0},{-1,1}}; 


	int end_x=a,end_y=b;
	initQueue(&Q);
	QElemType e,e1;
	e.pre=-1;	e.x=1;	e.y=1;
	
	enQueue(&Q,e);
	
	while(!(Q.front==Q.rear)){
	   int i,x,y;
	   deQueue(&Q,&e);
	   for(i=0;i<8;i++){
		   	x=e.x+delta[i][0];
		   	y=e.y+delta[i][1];
		   	
		   	if(m[x][y]==0){//如果可以走
		   		//形成新元素入队,标记走过-->2 
		   		e1.x=x;	e1.y=y;	e1.pre=Q.front-1;
		   		enQueue(&Q,e1);
		   		m[x][y]=2;
		   		if(x==end_x&&y==end_y){
		   		 printf("路径:\n");
		   		 printq(Q,Q.rear-1);return 0;
				}
			}
		   		
	   }
	}
	printf("无法通过!");
	return 0;
}


运行结果:


队列实现迷宫Java代码的主要思路如下: 1. 首先,创建一个二维数组来表示迷宫的布局,其中1代表墙壁,0代表通道。 2. 创建一个队列来保存迷宫中待探索的位置。 3. 将起点(1, 1)加入队列,并将其标记为已访问。 4. 进入循环,直到队列为空或找到出口(m, n)为止: - 从队列中取出一个位置(x, y)。 - 检查该位置的上、下、左、右四个邻居是否为通道且未访问过。 - 如果是,则将邻居位置加入队列,并标记为已访问。 - 继续下一次循环。 5. 如果找到出口(m, n),则通过回溯找到从起点到出口的路径。 6. 如果队列为空,表示无法找到通往出口的路径。 下面是一段示例代码,用于实现队列迷宫的功能: ```java import java.util.LinkedList; import java.util.Queue; public class MazeSolver { private int[][] maze; private boolean[][] visited; private int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; public MazeSolver(int[][] maze) { this.maze = maze; this.visited = new boolean[maze.length][maze[0].length]; } public boolean solve() { int m = maze.length; int n = maze[0].length; Queue<int[]> queue = new LinkedList<>(); queue.offer(new int[]{1, 1}); visited[1][1] = true; while (!queue.isEmpty()) { int[] position = queue.poll(); int x = position[0]; int y = position[1]; if (x == m && y == n) { return true; // 找到出口 } for (int[] direction : directions) { int newX = x + direction[0]; int newY = y + direction[1]; if (newX >= 1 && newX <= m && newY >= 1 && newY <= n && maze[newX][newY] == 0 && !visited[newX][newY]) { queue.offer(new int[]{newX, newY}); visited[newX][newY] = true; } } } return false; // 无法找到通往出口的路径 } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值