第十届蓝桥杯省赛JavaB组——第五题迷宫

题目:

试题 E: 迷宫
本题总分:15 分
【问题描述】
下图给出了一个迷宫的平面图,其中标记为 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
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一
个字符串,包含四种字母 D、U、L、R,在提交答案时只填写这个字符串,填
写多余的内容将无法得分。

Java代码:

  1 package com.lzp.lanqiaoten.p5;
  2 
  3 import java.util.LinkedList;
  4 import java.util.Queue;
  5 
  6 public class MazeMinPath {
  7 
  8     //存储整个迷宫
  9     int[][] map;
 10 
 11     // 方向由两个一维数组表示 ----    下左右上
 12     int[] dY = {0, -1, 1, 0};
 13     int[] dX = {1, 0, 0, -1};
 14 
 15     public MazeMinPath(int[][] map) {
 16         this.map = map;
 17         //先对整个dist数组赋值为-1,这样可以方便我们判断接下来的要走的邻接结点是否走过!!
 18         for (int i = 0; i < 30; i++) {
 19             for (int j = 0; j < 50; j++) {
 20                 dist[i][j] = -1;
 21             }
 22         }
 23     }
 24 
 25     //用于存储我们的最短路径,也是每个结点到达终点的路径长度
 26     int[][] dist = new int[30][50];
 27 
 28     int n = 30, m = 50;
 29 
 30     /*
 31         D、U、L、R 分别表示向下、向上、向左、向右走。
 32         D<L<R<U
 33 
 34             BFS广度优先搜索
 35      */
 36     public void BFS() {
 37         // 用一个队列存储当前的位置结点(结点数据结构在下面)
 38         Queue<Node> queue = new LinkedList<>();
 39         //(初始化起点)这里我们选择倒着走整个迷宫,从终点开始走到起点
 40         dist[n - 1][m - 1] = 0;
 41         //首先添加起点的结点
 42         queue.add(new Node(n - 1, m - 1));
 43         //开始BFS
 44         while (queue.size() != 0) {
 45             //出队列并由node保存该结点
 46             Node node = queue.poll();
 47             for (int i = 0; i < 4; i++) {
 48                 /*
 49                     i=0 下
 50                     i=1 左
 51                     i=2 右
 52                     i=3 上
 53                 */
 54                 int x = node.x + dX[i];
 55                 int y = node.y + dY[i];
 56                 //该方向是否存在一个通的道路  (即map[x][y] == 0)
 57                 if (x >= 0 && y >= 0 && x < n && y < m && dist[x][y] == -1 && map[x][y] == 0) {
 58                     //路径更新
 59                     dist[x][y] = dist[node.x][node.y] + 1;
 60                     //把通的结点 入列
 61                     queue.add(new Node(x, y));
 62                 }
 63             }
 64         }
 65     }
 66 }
 67 
 68 class Node {
 69     int x;
 70     int y;
 71 
 72     public Node(int x, int y) {
 73         this.x = x;
 74         this.y = y;
 75     }
 76 }
 77 
 78 class Text {
 79     public static void main(String[] args) {
 80         // 首先构建一个迷宫
 81         String maze = "010101010010110010010101100101101001000010001010100000100010000010101001000010000000100110011010010101111011010010001000001101001011100011000000010000010000000010101000110100001010000010101010110010110001111100000010100001001010001010000010110000000011001000110101000010101100011010011010101011110111000110110101010010010010100000010001010011100000001010000010100010011010101011111001100001000011101000111000001010100001100010000001000101001100001001110001101000011100100010010101010101010100011010000001000010010000010100101010111010001010101000010111100100101001001000010000010101010100100100010100000000100000001010110011110100011000001010101000111010101001110000100001100001011001111011010000100010101010100001101010100101000010100000111011101001100000001011000100001011001011010010111000000001001010100100000001010010000100010000010001111010100100101001010101101001010100011010101101110000110101110010100001000011000000101001010000010001110000100000100011000011010110100000010010100100100001110110100101000101000000001110110010110101101010100001001010000100001101010100001000100010010001000101011010000100011001000100001010100101010101111101001000000100101000000110010100101001000001000000000010110100000010011101110010010000111010010110111010000000011010001000100010000000100001110100000011001110101000101000100010001111100010101001010000001000100000101001010010101100000001001010100010111010000011110000100001000000011011100000000100000000101110000001100111010111010001000110111010101101111000";
 82         int count = 0;
 83         int[][] map = new int[30][50];
 84         for (int i = 0; i < 30; i++) {
 85             for (int j = 0; j < 50; j++) {
 86                 map[i][j] = Integer.parseInt(maze.substring(count, count + 1));
 87                 count++;
 88             }
 89         }
 90 //        for (int i = 0; i < 30; i++) {
 91 //            for (int j = 0; j < 50; j++) {
 92 //                System.out.print(map[i][j]);
 93 //            }
 94 //            System.out.println();
 95 //        }
 96         MazeMinPath mazeMinPath = new MazeMinPath(map);
 97         mazeMinPath.BFS();
 98         //走到[0][0]即为起点,也就是走完了整个迷宫
 99         //System.out.println(mazeMinPath.dist[0][0]);
100         //接下来求出路径
101         int x = 0, y = 0;
102         // D<L<R<U
103         String[] path = {"D", "L", "R", "U"};
104         String Path = "";
105 //        for (int i = 0; i < 30; i++) {
106 //            for (int j = 0; j < 50; j++) {
107 //                System.out.printf("%4d", mazeMinPath.dist[i][j]);
108 //            }
109 //            System.out.println();
110 //        }
111         int[] dY = {0, -1, 1, 0};
112         int[] dX = {1, 0, 0, -1};
113         // 没有走到终点,就一直循环下去
114         while (x != 29 || y != 49) {
115             for (int i = 0; i < 4; i++) {
116                 /*
117                     i=0 下
118                     i=1 左
119                     i=2 右
120                     i=3 上
121                 */
122                 int nx = x + dX[i];
123                 int ny = y + dY[i];
124                 // 满足以下第一个if语句,只能说该点可走,但是不是最佳选择
125                 if (nx < 30 && nx >= 0 && ny < 50 && ny >= 0 && map[nx][ny] == 0) {
126                     // 如果满足第二个if语句,表示此时所选的下一个即将要走的点是离当前点最近的点(因为0是本身不算,只有1最近)
127                     // 而且还是按照下、左、右、上这样的顺序来走的,就完全符合题意了。
128                     if (mazeMinPath.dist[x][y] == mazeMinPath.dist[nx][ny] + 1) {
129                         x = nx;
130                         y = ny;
131                         Path += path[i];
132                         //System.out.println(Path);
133                         // 找到就跳出,不需要再判断其他方向可不可行,因为我们就是从字典序中最小的那个方向对应的字符开始找的
134                         break;
135                     }
136                 }
137             }
138         }
139 
140         // 这就是最终结果
141         System.out.println(Path);
142     }
143 }

答案:

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

 

posted @ 2021-03-13 20:43  没有你哪有我  阅读(365)  评论(0编辑  收藏  举报