B - 迷宫问题(BFS)(Java实现)

在这里插入图片描述
队列不能用STL的queue或deque,要自己写。用一维数组实现,维护一个队头指针和队尾指针


import java.util.Scanner;

public class Main {

	static int[][]  map=new int[5][5];
	//考虑到保存路径,所以就没有直接用队列,
	//而是用数组模拟的队列,这样能够将东西都保存起来而不弹出
	static step[] queue=new step[25];
	static class step{
		int x,y,pre;//pre存储父节点 的数组下标作为指针
		public step(int x,int y,int pre) {
			this.x=x;
			this.y=y;
			this.pre=pre;
		}
		public int getX() {
			return x;
		}
		public int getY() {
			return y;
		}
		public int getPre() {
			return pre;
		}
		
	}
	static int[][] move = {{0,1},{0,-1},{1,0},{-1,0}};//移动的方向
	static int[][] visit=new int[5][5];
	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				map[i][j]=reader.nextInt();
		int head=0,tail=0;
		queue[head]=new step(0, 0,-1);
		visit[0][0]=1;
		tail++;
		while(head<tail) {
			boolean flag=false;
			for(int i=0;i<4;i++) {
				int nx=queue[head].getX()+move[i][0];
				int ny=queue[head].getY()+move[i][1];
				if(nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && visit[nx][ny] != 1 && map[nx][ny] != 1) {
					visit[nx][ny]=1;
					queue[tail]=new step(nx, ny, head);
					tail++;
				}
				if(nx==4&&ny==4) {
					flag=true;
					break;
				}
			}
			if(flag) {
				print(queue[tail-1]);
				break;
			}
			head++;//假装弹出队列
		}
		
	}
	static void  print(step s) {
		if(s.pre==-1) {
			System.out.println("("+s.getX()+", "+s.getY()+")");
			return;
		}
		else {
			print(queue[s.getPre()]);
			System.out.println("("+s.getX()+", "+s.getY()+")");
		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值