【蓝桥杯java】迷宫(BFS)

在这里插入图片描述
maze.txt文件如下:

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

题解

宽度优先搜索 利用队列先进先出
宽度优先搜索求得的路径必然是最短路径,因为队列的特点先进先出最短的路径必然最先到达终点

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

class point {
	public int x;
	public int y;
	public String road = "";	//存储路径
	point(int a, int b) {
		x = a;
		y = b;
	}
}
public class Main {
	static int m = 30, n = 50;
	static char[][] map = new char[m][n];
	static int[][] vis = new int[m][n];		//标记是否访问
	static int dir[][] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};	//DLRU
	static String road[] = {"D", "L", "R", "U"};
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);		
		for(int i = 0; i < m; i++) {
			//输入迷宫信息
			map[i] = scan.next().toCharArray();
		}
		bfs();
	}
	
	static void bfs() {
		Queue<point> q = new LinkedList<>();
		q.add(new point(0, 0));
		vis[0][0] = 1;
		while(!q.isEmpty()) {
			point p = q.peek();
			int x = p.x;
			int y = p.y;
			q.poll();
			if(x == m - 1 && y == n - 1) {
				System.out.print(p.road);
				break;
			}
			for(int i = 0; i < 4; i++) {
				int dx = x + dir[i][0];
				int dy = y + dir[i][1];
				if(dx < 0 || dx > m - 1 || dy < 0 || dy > n - 1 || vis[dx][dy] == 1 || map[dx][dy] == '1') {
					continue;
				}
				point p1 = new point(dx, dy);
				//上一个点的总路径加上本次路径
				p1.road = p.road + road[i];
				q.add(p1);
				vis[dx][dy] = 1;
			}
		}
	}
}
### Java蓝桥杯迷宫问题真题及解法 #### 题目背景 蓝桥杯竞赛中的迷宫问题是典型的算法题目之一,通常涉及路径规划、图遍历等内容。这类问题的核心在于如何利用数据结构和算法来模拟迷宫环境并找到最优解决方案。 --- #### 迷宫问题描述 根据引用内容[^2],蓝桥杯第十三届省赛中提到的迷宫问题可以总结如下: 给定一个二维字符矩阵表示的迷宫地图,其中 `*` 表示墙壁(不可通行),`1` 或其他特定符号表示可行的道路,而未被标记的位置则默认为无法到达的区域。目标是从起点出发,计算能够达到终点的有效路径数量或者最短路径长度。 此类问题的关键点包括但不限于以下几个方面: - **输入处理**:读取迷宫的地图布局。 - **状态定义**:明确当前节点的状态以及其可达性。 - **边界条件判断**:防止越界访问或重复访问同一位置。 - **搜索策略实现**:采用广度优先搜索 (BFS) 或深度优先搜索 (DFS),具体取决于题目需求。 以下是基于上述分析的一个通用解决框架: ```java import java.util.LinkedList; import java.util.Queue; public class MazeSolver { private static final char WALL = '*'; // 墙壁标志 private static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上下左右移动方向 public static boolean isValid(int x, int y, char[][] maze) { return x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] != WALL; } public static int shortestPath(char[][] maze, int startX, int startY, int endX, int endY) { Queue<int[]> queue = new LinkedList<>(); queue.add(new int[]{startX, startY}); int rows = maze.length; int cols = maze[0].length; int[][] distance = new int[rows][cols]; for (int i = 0; i < rows; ++i) Arrays.fill(distance[i], Integer.MAX_VALUE); distance[startX][startY] = 0; while (!queue.isEmpty()) { int[] currentPos = queue.poll(); int cx = currentPos[0]; int cy = currentPos[1]; if (cx == endX && cy == endY) break; for (int[] dir : DIRECTIONS) { int nx = cx + dir[0]; int ny = cy + dir[1]; if (isValid(nx, ny, maze)) { if (distance[nx][ny] > distance[cx][cy] + 1) { distance[nx][ny] = distance[cx][cy] + 1; queue.offer(new int[]{nx, ny}); } } } } return distance[endX][endY] == Integer.MAX_VALUE ? -1 : distance[endX][endY]; } public static void main(String[] args) { char[][] maze = { {'*', ' ', '*', ' '}, {' ', ' ', ' ', '*' }, {'*', ' ', '*', ' '} }; int result = shortestPath(maze, 0, 1, 2, 1); System.out.println(result); // 输出结果应为实际步数 } } ``` 此代码片段实现了基本的 BFS 方法用于求解迷宫中最短路径问题。它通过队列维护待探索节点,并逐步扩展直至抵达目的地为止。 --- #### 关键技术要点 1. **数据存储形式** 使用二维数组保存迷宫信息是最常见的做法,便于索引操作且直观易懂。 2. **搜索方法选择** 对于寻找最短路径场景推荐使用 BFS;如果仅需验证是否存在某种通路,则 DFS 可能更加高效简单[^3]。 3. **优化技巧** - 提前终止不必要的分支运算以提高效率。 - 利用记忆化手段避免冗余计算。 --- #### 总结 综上所述,在面对类似蓝桥杯这样的编程比赛中遇到有关迷宫类别的考题时,掌握好基础理论知识加上灵活运用各种工具将会事半功倍。以上提供了一个较为完整的思路指导及其对应实例演示供参考学习之用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值