2019年第十届蓝桥杯省赛真题 迷宫 Java

本文介绍了一种使用广度优先搜索(BFS)算法解决复杂迷宫问题的方法,相比于深度优先搜索(DFS),BFS在处理大型迷宫时能更快找到最短路径。在给定的迷宫实例中,BFS成功找到了步数最少的解决方案,并通过字典序排序给出了最优路径。此外,文章还对比了DFS和BFS在解决此类问题上的性能差异。
摘要由CSDN通过智能技术生成

迷宫
题目描述
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这个它的上、下、左、右四个方向之一。对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫,一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。

对于下面这个更复杂的迷宫(30 行 50 列) ,请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。

请注意在字典序中D<L<R<U。(如果你把以下文字复制到文本文件中,请务必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 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

分析:这种题一般为搜索题,可以用BFS(广度优先搜索)和DFS(深度优先搜索算法)

解法一:DFS(超时了,看个乐呵)

package labyrinth;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 迷宫问题(dfs超时)
 * 
 * @author zmz
 *
 */
public class Labyrinth1 {
	static int[][] f = { { 1, 0 }, { -1, 0 }, { 0, -1 }, { 0, 1 } }; // 每个位置走的四个方向
	static char[] ch = { 'D', 'U', 'L', 'R' }; // 记录走的方向
	static int res = Integer.MAX_VALUE;
	static int[][] arr = {
			{0,1,0,0,0,0},
			{0,0,0,1,0,0},
			{0,0,1,0,0,1},
			{1,1,0,0,0,0}	   
	};

	static boolean[][] flag = new boolean[arr.length][arr[0].length]; // 标记该位置是否已经走过
	static StringBuilder sb = new StringBuilder();
	static List<String> dict = new ArrayList<>();

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = null;
		try {
			scanner = new Scanner(System.in);
			dfs(0, 0, 0);
			System.out.println("步数为 " + res);

			// 根据字典序排序
			dict.sort((o1, o2) -> {
				if (o1.length() == o2.length()) {
					char[] c1 = o1.toCharArray();
					char[] c2 = o2.toCharArray();

					for (int i = 0; i < o1.length(); i++) {
						if (c1[i] > c2[i])
							return 1;
						if (c1[i] < c2[i])
							return -1;
					}
				}
				return o1.length() - o2.length();
			});

			System.out.println(dict);
			System.out.println(dict.get(0));
		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	/**
	 * 
	 * @param x    当前在数组中的第 x 行
	 * @param y    当前在数组中的第 y 列
	 * @param step 步数
	 */
	private static void dfs(int x, int y, int step) {
		if (x == arr.length - 1 && y == arr[0].length - 1) {
			res = Math.min(res, step);
			dict.add(sb.toString()); // 将结果存到字典
			return;
		}

		for (int i = 0; i < f.length; i++) {
			int newX = x + f[i][0];
			int newY = y + f[i][1];
			if (newX >= 0 && newX < arr.length && newY >= 0 && newY < arr[0].length && arr[newX][newY] != 1
					&& !flag[newX][newY]) { // 这步走得通
				flag[newX][newY] = true;
				sb.append(ch[i]);
				dfs(newX, newY, step + 1);
				flag[newX][newY] = false;
				sb.deleteCharAt(step);
			}
		}
	}
}

只有小数据量能过
输出结果:
在这里插入图片描述

解法二:BFS(秒出结果)

package labyrinth;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

public class Labyrinth2 {

	static int[][] arr = {
			{0,1,0,1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,0,1,0,1,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,0,0,0,1,0,0,0,1,0,1,0,1,0},
			{0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,0,1,0,0,1,0,1},
			{0,1,1,1,1,0,1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0},
			{0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,1,1},
			{0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,0},
			{1,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,0,0,1,0,1,0,1,1,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,1,1,1,0,1,1,1},
			{0,0,0,1,1,0,1,1,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,1,1,1,0,0,0,0,0,0,0},
			{1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,1,1,0,1,0,1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,0,1,0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,0,0,0,1,0,1,1,1,0,1,0,0,0},
			{0,0,1,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,1},
			{1,0,0,0,0,0,0,1,1,0,0,1,1,1,0,1,0,1,1,1,0,1,0,0,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,1,0,0,0}
	};

	static int a = arr.length;
	static int b = arr[0].length;
	static int[][] f = { { 1, 0 }, { -1, 0 }, { 0, -1 }, { 0, 1 } };
	static char[] ch = { 'D', 'U', 'L', 'R' }; // 正常顺序
	static int res = Integer.MAX_VALUE;
	static boolean[][] flag = new boolean[a][b];
	static Pose[][] pF = new Pose[a][b];

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Deque<Pose> deque = new ArrayDeque<>();
		Stack<Character> stack = new Stack<>();
		Pose pose = new Pose(0, 0, 0, 'O'); // 原点
		deque.add(pose);
		flag[0][0] = true;
		pF[0][0] = pose;

		while (!deque.isEmpty()) {
			Pose pop = deque.pop();
			for (int i = 0; i < 4; i++) {
				int newX = pop.x + f[i][0];
				int newY = pop.y + f[i][1];
				if (newX >= 0 && newX < a && newY >= 0 && newY < b && !flag[newX][newY] && arr[newX][newY] != 1) {
					// 可行点
					Pose p = new Pose(newX, newY, pop.step + 1, ch[i]);
					pF[newX][newY] = p;
					flag[newX][newY] = true;
					deque.add(p);
				}
			}
		}

		/*
		 * 输出地图全貌,位置值的含义为上一个点是从哪个方向过来该位置的
		 */
		for (Pose[] i : pF) {
			for (int j = 0; j < i.length; j++) {
				if (i[j] != null) {
					System.out.print(i[j].lastP);
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}

		// 倒退回去,用栈保存
		a = a - 1;
		b = b - 1;
		stack.push(pF[a][b].lastP);
		while (a != 0 || b != 0) { // 求路径
			switch (pF[a][b].lastP) {
			case 'D':
				a -= 1; // 上一个节点向下走到该位置, 要回到上一个节点, 从该位置向上走
				stack.push(pF[a][b].lastP);
				break;
			case 'U':
				a += 1;
				stack.push(pF[a][b].lastP);
				break;
			case 'L':
				b += 1;
				stack.push(pF[a][b].lastP);
				break;
			case 'R':
				b -= 1;
				stack.push(pF[a][b].lastP);
				break;
			}

		}
		stack.pop(); // 去掉原点
		while (!stack.isEmpty()) {
			System.out.print(stack.pop());
		}
	}

}

class Pose {
	int x; // 位置的横坐标
	int y; // 位置的纵坐标
	int step; // 从原点到该位置的最小距离
	char lastP; // 上一个点到该点的方向

	public Pose() {
	}

	public Pose(int x, int y, int step, char lastP) {
		this.x = x;
		this.y = y;
		this.step = step;
		this.lastP = lastP;
	}
}

输出结果(太多了,放不下)
在这里插入图片描述
用Excel试了一下,路径是这样子的
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值