问题描述

问题解答

package com.leetcode.other;

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

 

09-13 16:50