试题 E: 迷宫 本题总分:15 分

  • Post author:
  • Post category:其他

【问题描述】 下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方。 010000 000100 001001 110000 迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。 对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式, 其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。 请注意在字典序中D

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

 

 

Java中Scanner类中的方法next()和nextLine()都是吸取输入台输入的字符,区别:

  •  next()不会吸取字符前/后的空格/Tab键,只吸取字符,开始吸取字符(字符前后不算)直到遇到空格/Tab键/回车截止吸取;
  •  nextLine()吸取字符前后的空格/Tab键,回车键截止。

1.BFS是用来搜索最短径路的解是比较合适的,比如求最少步数的解,最少交换次数的解,因为BFS搜索过程中遇到的解一定是离根最近的,所以遇到一个解,一定就是最优解,此时搜索算法可以终止。这个时候不适宜使用DFS,因为DFS搜索到的解不一定是离根最近的,只有全局搜索完毕,才能从所有解中找出离根的最近的解。(当然这个DFS的不足,可以使用迭代加深搜索ID-DFS去弥补)
2.空间优劣上,DFS是有优势的,DFS不需要保存搜索过程中的状态,而BFS在搜索过程中需要保存搜索过的状态,而且一般情况需要一个队列来记录。
3.DFS适合搜索全部的解,因为要搜索全部的解,那么BFS搜索过程中,遇到离根最近的解,并没有什么用,也必须遍历完整棵搜索树,DFS搜索也会搜索全部,但是相比DFS不用记录过多信息,所以搜素全部解的问题,DFS显然更加合适。 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
	static Scanner input = new Scanner(System.in);

	public static int dir[][] = new int[][] { { 1, 0 }, { 0, -1 }, { 0, 1 }, { -1, 0 } };
	public static char direction[] = new char[] { 'D', 'L', 'R', 'U' };
	public static int[][] map = new int[30][50];
	public static boolean[][] visit = new boolean[30][50];

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);
		for (int i = 0; i < 30; i++) {
			String s = in.next();  //必须为next(),不能是nextLine()
			for (int j = 0; j < 50; j++) {
				map[i][j] = s.charAt(j) - '0';
			}
		}

		bfs(new Point(0, 0));
	}

	public static void bfs(Point start) {
		Queue<Point> q = new LinkedList<Point>();
		q.add(start);
		visit[start.i][start.j] = true;
		while (!q.isEmpty()) {
			Point p = q.peek();
			if (p.i == 29 && p.j == 49) {
				System.out.println(p.step);
				System.out.println(p.way);
			}

			for (int i = 0; i < 4; i++) {
				int new_i = p.i + dir[i][0];
				int new_j = p.j + dir[i][1];
				Point temp = new Point(new_i, new_j);
				if (new_i >= 0 && new_j >= 0 && new_i < 30 && new_j < 50 && !visit[new_i][new_j]
						&& map[new_i][new_j] != 1) {
					visit[new_i][new_j] = true;
					temp.step = p.step + 1;
					temp.way = p.way + direction[i];
					q.add(temp);
				}
			}
			q.poll();
		}

	}

}

class Point {
	int i;
	int j;
	int step;
	String way;

	public Point() {
		i = j = step = 0;
		way = "";
	}

	public Point(int i, int j) {
		this.i = i;
		this.j = j;
		step = 0;
		way = "";
	}
}


版权声明:本文为weixin_44936687原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。