5.迷宮
下圖給出了一個迷宮的平面圖,其中標記為1 的為障礙,標記為0 的為可
以通行的地方,010000 000100 001001 110000
迷宮的入口為左上角,出口為右下角,在迷宮中,只能從一個位置走到這
個它的上、下、左、右四個方向之一,
對于上面的迷宮,從入口開始,可以按DRRURRDDDR 的順序通過迷宮,
一共10 步,其中D、U、L、R 分別表示向下、向上、向左、向右走,對于下面這個更復雜的迷宮(30 行50 列),請找出一種通過迷宮的方式,
其使用的步數最少,在步數最少的前提下,請找出字典序最小的一個作為答案,
請注意在字典序中D<L<R<U01010101001011001001010110010110100100001000101010 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
思路
-
撰寫方法將長的要死的字串自動轉換為二維int陣列
-
BFS
-
使用佇列計算不同路徑的長度
-
最后走最短路徑并拼接回傳的字串
DIS陣列效果圖
左:

右:

package lanqiao;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.function.IntPredicate;
public class Main {
static int n = 30,m = 50;
//迷宮陣列
static int[][] maze = new int[n][m];
//路徑字串
static String path = "";
static int[][] dir = {{1,0},{0,-1},{0,1},{-1,0}};
//距離陣列
static int[][] dis = new int[n][m];
//動作代表標識 可能有的小伙伴會問為什么不是URLD
//這是因為一開始我們是從終點往起點走 所以是反過來的
static char[] c = {'D','L','R','U'};
public static void main(String[] args) {
maze = convert(
"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",n,m);
solution (maze);
System.out.println(path);
prinf(dis);
}
public static void solution(int [][] way) {
Queue<Integer> queue = new LinkedList<>();
//n * m - 1是終點的坐標計算出的唯一標識
//以此類推 起點(第一行第一列)的唯一標識1*1-1為0
queue.add(n * m - 1);
while(!queue.isEmpty()) {
Integer poll = queue.poll();
for(int i = 0; i < 4; i++) {
//獲取佇列中元素對應的行和列
int xx = poll / m;
int yy = poll % m;
//上/下/左/右動一步
xx = xx + dir[i][0];
yy = yy + dir[i][1];
//檢測能不能走 能走就加入佇列 dis[][]不為0代表已經走過了
if(xx < 0 || yy < 0 || xx >= n || yy >= m || maze[xx][yy] == 1 || dis[xx][yy] != 0 ) {
continue;
}
queue.add(xx * m + yy);
dis[xx][yy]= dis[poll / m][poll % m] + 1;
if(xx == 0 && yy == 0) {
//該條路徑反向找到起點,結束
break;
}
}
}
//若無這一步 最后將進入死回圈
dis[n-1][m-1] = 0;
//從起點開始往終點 走最短路徑獲得行進字串
int x = 0, y = 0;
while(x != n-1 || y != m-1) {
for(int i = 0; i < 4; i++) {
//與上類似
int xx = x + dir[i][0];
int yy = y + dir[i][1];
//不能走則跳過 此處不能加上dis[xx][yy] == 0條件 因為最后會無法走到終點
if(xx == -1 || yy == -1 || xx == n || yy == m || maze[xx][yy] == 1) {
continue;
}
//這里不是判斷dis[xx][yy] = dis[x][y] + 1的原因是我們使用的策略是距離終點越遠dis越大
if(dis[x][y] == dis[xx][yy] + 1) {
//匹配成功 移動
x = xx;
y = yy;
//記錄路徑
path += c[i];
//已找到對應路徑
break;
}
}
}
}
public static int[][] convert(String args,int x,int y){
int[][] res = new int[x][y];
for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
res[i][j] = args.charAt(j + i * y) - 48;
}
}
return res;
}
public static void prinf(int [][] res) {
for(int[] a : res) {
for(int b : a) {
System.out.printf("%3d ",b);
}
System.out.println();
}
System.out.println();
}
}
答案
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/272876.html
標籤:java
上一篇:普歌-允異團隊-【演算法思路】從入題至解題的詳細思路講解——LeetCode——兩數之和
下一篇:邏輯與控制(Java)學習總結
