問題描述
試題 E: 迷宮
【問題描述】
下圖給出了一個迷宮的平面圖,其中標記為 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
問題解答
import java.util.LinkedList;
import java.util.List;
/**
* 第十屆藍橋杯省賽java類B組 試題 E:迷宮
* 動態規劃之回溯法
* 執行策略:0:通路,但是沒走過 1:障礙物,無法通過 2:走過了,通路 3:走過了,但是不通
* 優先順序:下 右 上 左
*/
public class Maze {
public static void main(String[] args) {
int[][] map = structureMap();
List list = new LinkedList<String>();
// foreachMap(map);
walk(list, map, 0, 0);
foreachMap(map);
StringBuilder result = new StringBuilder();
// 遞回之后操作是自底向上,需要倒序排序
for (int i=list.size()-1;i>=0;i--) {
result.append(list.get(i));
}
System.out.println(result);
}
public static boolean walk(List list, int[][] map, int i, int j) {
int rows = map.length;
int cols = map[0].length;
// 遞回出口
if(map[rows-1][cols-1] == 2) {
return true;
} if(map[i][j] != 0){
return false;
}
// 先假設當前位置可以走通
map[i][j] = 2;
if(i+1<rows && walk(list, map, i+1, j)) {
list.add("D");
return true;
} if (j+1<cols && walk(list, map, i, j+1)) {
list.add("R");
return true;
} if (i-1>=0 && walk(list, map, i-1, j)) {
list.add("U");
return true;
} if (j-1>=0 && walk(list, map, i, j-1)) {
list.add("L");
return true;
}
// 回溯:走不通則重置為3
map[i][j] = 3;
return false;
}
/**
* 遍歷迷宮
* @param map
*/
public static void foreachMap(int[][] map) {
int rows = map.length;
int cols = map[0].length;
for(int i=0;i<rows;i++) {
for(int j=0;j<cols;j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
System.out.println("--------------------------------------------------");
}
/**
* 構造迷宮
* @return
*/
public static int[][] structureMap() {
String maze =
"01010101001011001001010110010110100100001000101010\n" +
"00001000100000101010010000100000001001100110100101\n" +
"01111011010010001000001101001011100011000000010000\n" +
"01000000001010100011010000101000001010101011001011\n" +
"00011111000000101000010010100010100000101100000000\n" +
"11001000110101000010101100011010011010101011110111\n" +
"00011011010101001001001010000001000101001110000000\n" +
"10100000101000100110101010111110011000010000111010\n" +
"00111000001010100001100010000001000101001100001001\n" +
"11000110100001110010001001010101010101010001101000\n" +
"00010000100100000101001010101110100010101010000101\n" +
"11100100101001001000010000010101010100100100010100\n" +
"00000010000000101011001111010001100000101010100011\n" +
"10101010011100001000011000010110011110110100001000\n" +
"10101010100001101010100101000010100000111011101001\n" +
"10000000101100010000101100101101001011100000000100\n" +
"10101001000000010100100001000100000100011110101001\n" +
"00101001010101101001010100011010101101110000110101\n" +
"11001010000100001100000010100101000001000111000010\n" +
"00001000110000110101101000000100101001001000011101\n" +
"10100101000101000000001110110010110101101010100001\n" +
"00101000010000110101010000100010001001000100010101\n" +
"10100001000110010001000010101001010101011111010010\n" +
"00000100101000000110010100101001000001000000000010\n" +
"11010000001001110111001001000011101001011011101000\n" +
"00000110100010001000100000001000011101000000110011\n" +
"10101000101000100010001111100010101001010000001000\n" +
"10000010100101001010110000000100101010001011101000\n" +
"00111100001000010000000110111000000001000000001011\n" +
"10000001100111010111010001000110111010101101111000";
// System.out.println(maze);
String[] S = maze.split("\n");
int rows = S.length;
int cols = S[0].length();
// System.out.println(rows+" "+cols);
int[][] Ints = new int[rows][cols];
for(int i=0;i<rows;i++) {
// System.out.println(S[i]);
String str = S[i];
char[] chars = str.toCharArray();
for(int j=0;j<chars.length;j++) {
Ints[i][j] = Integer.valueOf(chars[j]) - 48;
}
}
return Ints;
}
}
運行結果
21010101001011001001010110010110100122221000101010
20001000100000101010010000100000001221120110100101
21111011010010001000001101001011100211022222210000
21222222201010100011010000101002221213131311201011
22211111222220101000010010100012122233131100222000
11331000113121000010101100011012211313131011112111
33311011013121001001001010000001233131331112222333
13100000101223100110101010111110211333310002111313
33111000001213100001100010000001222131331102221331
11000110100221110010001001010101312131310001121333
00010000100120000101001010101110102313101010022131
11100100101021001000010000010101012122100100012133
00000010000020101011001111010001102222101010122311
10101010011120001000011000010110011112110100021333
10101010100021101010100101000010122222111011121331
10000000101120010000101100101101021311100002223133
10101001000020010100100001000100023133311112131331
00101001010121101001010100011010121131112222110131
11001010000120001100000010100101022221022111000010
00001000110020110101101000000100131321021000011101
10100101000121000022201110110010110121121010100001
00101000010022110121210000100010001021223100010101
10100001000112012221200010101001010121211111010010
00000100101002222110210100101001000021200222222210
11010000001001113111221001000011101021211211131200
00000110100010001000120000001222011121200222110211
10101000101000100012221111102212101221210222221200
10000010100101001012112222222102101210201311121200
00111100001000010002222113111002222231222222221211
10000001100111010111313331000113111310131131111222
--------------------------------------------------
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDDLLDDRRRURRRRRRURURRDDDRRRRUURUUUUUUUULLLUUUURRRRUUULDLLUUUULLUUULUURRDRRUUURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDDDDDDRRRRRRRUULLULDLUUURRRRRRDDDDDDRRU
最終答案
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDDLLDDRRRURRRRRRURURRDDDRRRRUURUUUUUUUULLLUUUURRRRUUULDLLUUUULLUUULUURRDRRUUURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDDDDDDRRRRRRRUULLULDLUUURRRRRRDDDDDDRRU
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/19117.html
標籤:其他
上一篇:力扣Java版個人代碼分享-樹篇( 107. 二叉樹的層次遍歷 II)
下一篇:鴻蒙OS代碼正式開源
