/*試題 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 試題E: 迷宮 7 第十屆藍橋杯大賽軟件類省賽 Java 大學 B 組 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,在提交答案時只填寫這個字符串,填 寫多余的內容將無法得分。*/
解題思路:
1、廣度優先搜索:遍歷從起點到終點 每一個點到重點的最短距離 逆向搜索
2、深度優先搜索:根據廣度優先搜索的預處理,深度搜索一條最短的路徑
答案正確
import java.util.*; public class E5{ static int n,m; static char[][] maze; static int[][] dis; static int[][] dir = {{1 ,0},{0,-1 },{0,1 },{-1 ,0}}; static char[] c = {'D','L','R','U'}; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Queue<Integer> queue = new LinkedList<Integer>(); dis = new int[30][50]; maze = new char[30][50]; n = scanner.nextInt(); m = scanner.nextInt(); for(int i=0;i<n;i++) { String string = scanner.next(); for(int j=0;j<m;j++) maze[i][j] = string.charAt(j); } queue.add((n-1 )*m+m-1 ); while(!queue.isEmpty()) { int temp = queue.poll(); for(int i=0;i<4;i++) { int xx = temp/m + dir[i][0]; int yy = temp%m + dir[i][1 ]; if(xx<0||xx>=n||yy<0|yy>=m||maze[xx][yy]=='1'||dis[xx][yy]!=0) continue; //注意這里dis[n-1 ][m-1 ]=0,但是已經遍歷過了 System.out.println(xx+" "+yy); queue.add(xx*m+yy); dis[xx][yy] = dis[temp/m][temp%m] + 1 ; if(xx==0&&yy==0) break; } } dis[n-1 ][m-1 ] = 0; //由於遍歷不止一遍,所以值不是0,要在定義一遍 String record=""; 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 ]; if(xx<0||xx>=n||yy<0|yy>=m||maze[xx][yy]=='1') continue; if(dis[x][y]==dis[xx][yy]+1 ) { x = xx; y = yy; record += c[i]; break; } } } System.out.println(record.length()); System.out.println(record); } }
//DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUUL
//ULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
最短路徑-->BFS
記錄路徑--> 結構體中String存儲路徑
答案錯誤
import java.util.*; public class Main{ static class node{ int x; int y; String path; public node(int x,int y,String path) { this.x = x; this.y = y; this.path = path; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Queue<node> queue = new LinkedList<node>(); int[][] dir = {{0 ,1 },{0 ,-1 },{-1 ,0 },{1 ,0 }}; char[] c = {'R','L','U','D'}; int[][] maze = new int[30 ][50 ]; int n = scanner.nextInt(); int m = scanner.nextInt(); for(int i=0 ;i<n;i++) for(int j=0 ;j<m;j++) maze[i][j] = scanner.nextInt(); maze[0 ][0 ] = 1 ; queue.add(new node(0 , 0 , "")); while(!queue.isEmpty()) { node temp = queue.poll(); for(int i=0 ;i<4;i++) { int xx = temp.x + dir[i][0 ]; int yy = temp.y + dir[i][1 ]; if(xx<0 ||xx>=n||yy<0 ||yy>=m||maze[xx][yy]==1 ) continue; maze[xx][yy] = 1 ; queue.add(new node(xx, yy, temp.path+c[i])); if(xx==n-1 &&yy==m-1 ) System.out.println(temp.path+c[i]); } } } }