蓝桥杯java 迷宫

 

 

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

 

这里用bfs从终点依次寻找到终点的最短距离,存放到一数组里面,最后输出起点数组就得到路径长度
而具体路径,就从头遍历开始,寻找长度相差一的,最后就是所要的答案,代码如下:

 1 import java.util.*;
 2 
 3 class Node{
 4     int x, y;
 5     void node(int x, int y) {
 6         this.x = x;
 7         this.y = y;
 8     }
 9 }
10 public class Main {
11     static int n,m;
12     static char [][] dir = new  char[30][50];
13     static Queue<Node> queue = new LinkedList<Node>();
14     static int a[] = {1, 0, 0, -1};
15     static int b[] = {0, -1, 1, 0};
16     static char s[] = {'D', 'L', 'R', 'U'};
17     static int [][] len = new int[30][50];
18     
19      static void bfs() {
20         Node s = new Node();
21         s.x = n - 1;
22         s.y = m - 1;
23         len[n - 1][m - 1] = 0;
24         queue.offer(s);
25         while(!queue.isEmpty()) {
26             s = queue.poll();
27             for(int i = 0; i < 4; i++) {
28                 int ax = s.x + a[i];
29                 int by = s.y + b[i];
30                 if(ax >= 0 && ax < n && by >= 0 && by < m && len[ax][by] == -1 && dir[ax][by] == '0') {
31                     len[ax][by] = len[s.x][s.y] + 1;
32                     Node node = new Node();
33                     node.x = ax;
34                     node.y = by;
35                     queue.offer(node);
36                 }
37             }
38         }
39 
40     }
41     
42     
43 public static void main(String[] args)
44 {
45     Scanner sc = new Scanner(System.in);
46     n = sc.nextInt();
47     m = sc.nextInt();
48     
49     for(int i = 0; i < n; i++) {
50         String s = sc.next();
51         for(int j = 0; j < m; j++) {
52             len[i][j] = -1;
53             dir[i][j] = s.charAt(j);
54         }
55     }
56     bfs();
57     System.out.println(len[0][0]);
58     int x = 0; int y = 0;
59     String p = "";
60     while(x != n-1 || y != m-1) {
61     W:    for(int i = 0; i < 4; i++) {
62             int ax = x + a[i];
63             int by = y + b[i];
64             if(ax >= 0 && ax < n && by >= 0 && by < m && len[ax][by] != -1 && dir[ax][by] == '0') {
65                 if(len[x][y] == len[ax][by] + 1) {
66                     x = ax;
67                     y = by;
68                     p += s[i];
69                     break W;
70                 }
71                     
72             }
73         }
74     }
75     System.out.print(p);
76     
77 }
78 }

没给注释,不懂可以留言,个人做的,有错误欢迎指正。

posted @ 2020-02-05 13:03  斑马还没睡  阅读(447)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end