Java解决迷宫问题(BFS+队列)

package _2_1递归;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class point{//一个点
	int x;
	int y;
	int temp;
}

public class 宽搜BFS迷宫问题 {
	
	public static int[][] a=new int[100][100];//迷宫
	public static int[][] v=new int[100][100];//访问数组
	public static int[] dx= {0,1,0,-1};//x方向上(竖着为x)扩展
	public static int[] dy= {1,0,-1,0};//y方向上(横着为y)扩展
	
	public static void main(String args[]) {
		Scanner s=new Scanner(System.in);
		int n,m;//n行m列的迷宫
		n=s.nextInt();
		m=s.nextInt();
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=m;j++){
				a[i][j]=s.nextInt();
			}
		}
		int startx,starty;//起点坐标
		startx=s.nextInt();
		starty=s.nextInt();
		int p,q;//终点坐标
		p=s.nextInt();
		q=s.nextInt();
		int flag=0;//判断能不能找到路径,初始设1,为找不到
		//BFS
		Queue<point> queue=new LinkedList<>();//创建一个队列
		point start=new point();//定义一个起点
		start.x=startx;
		start.y=starty;
		start.temp=0;
		queue.add(start);//将起点入队,起点为队首
		v[startx][starty]=1;//设置为该点已经访问
		//将队首在四个方向上(右下左上)所扩展的点进行入队
		while(!queue.isEmpty()) {//当队列为空时停止判断
			int x=queue.peek().x;
			int y=queue.peek().y;//取队首元素的的坐标
			if(x==p&&y==q) {//如果到达终点,就输出+退出循环
				flag=1;//找到这个路径了,flag设1
				System.out.print(queue.peek().temp);
				break;
			}else {//如果还没有到达终点,就扩展四个方向上的点
				for(int k=0;k<=3;k++) {
					int tx = 0,ty = 0;//临时拓展方向
					tx=x+dx[k];
					ty=y+dy[k];
					if(a[tx][ty]==1 && v[tx][ty]==0) {//如果迷宫里这个点可以走并且还没有被访问
						//申请一个临时的结点用于入队
						point temp = new point();
						temp.x=tx;
						temp.y=ty;
						temp.temp=queue.peek().temp+1;//往前走了,所以步数+1
						queue.add(temp);//将这个点放入队列
						v[tx][ty]=1;//将这个点设置为已访问
					}
				}
				queue.poll();//将队首元素出队
			}
		}
		if(flag==0) {
			System.out.print("no answer");
		}		
	}
}

总结:

1、队列的运用,创建队列的方法为Queue<(数据类型)> queue=new LinkedList<>(),插入队列的方法为add(),放出队列的方法为poll(),取队头元素的方法为peek()。

2、迷宫的每一个点都是一个类,都有坐标变量x、y和步长变量step。要知道类怎么创建,以及类内成员变量怎么调用。

3、宽度优先搜索的思维,往右下左上四个方向扩展找值,判断是不是我们要找的点,不是就按队头元素扩展之后放入队列,队头元素出队。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于Java迷宫问题BFS算法,我可以给您提供以下思路: 1. 首先需要定义一个二维数组来表示迷宫,0表示可以通过的路,1表示障碍物; 2. 定义一个队列来存储待访问的节点; 3. 将起点加入队列,并将其标记为已访问; 4. 对于队列中的每个节点,分别向上、下、左、右四个方向进行探索,如果该方向上的节点未被访问过且可以通过,则将其加入队列,并标记为已访问; 5. 直到队列为空或者找到终点为止。 以下是Java代码实现: ```java import java.util.LinkedList; import java.util.Queue; public class MazeBFS { public static int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上下左右四个方向 public static boolean bfs(int[][] maze, int[] start, int[] dest) { int m = maze.length, n = maze[0].length; boolean[][] visited = new boolean[m][n]; // 标记是否访问过 Queue<int[]> queue = new LinkedList<>(); queue.offer(start); visited[start[0]][start[1]] = true; while (!queue.isEmpty()) { int[] cur = queue.poll(); if (cur[0] == dest[0] && cur[1] == dest[1]) { return true; } for (int[] dir : directions) { int x = cur[0] + dir[0], y = cur[1] + dir[1]; if (x >= 0 && x < m && y >= 0 && y < n && !visited[x][y] && maze[x][y] == 0) { queue.offer(new int[]{x, y}); visited[x][y] = true; } } } return false; } public static void main(String[] args) { int[][] maze = {{0, 0, 1, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 1, 0}, {1, 1, 0, 1, 1}, {0, 0, 0, 0, 0}}; int[] start = {0, 4}, dest = {4, 4}; System.out.println(bfs(maze, start, dest)); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值