菲洛嘉青春动能素135HA FILLMED® NCTF 135HA LED指示灯的常见故障分析 智微智能 Elkhartlake K075终端,零售产业新选择 天空蓝拓客管理系统详细介绍版 muso公链项目 天使计划 是什么?[秘] 独家揭秘最前沿的家装“黑科技”——掌赋 天博体育欧洲杯特辑,东道主法兰西的失意2016 亚马逊的送货侦察员 学习听起来像挡泥板 Google Comics Factory使ML变得容易 笑着说-男性或女性 Amazon Rekognition中更好的人脸检测 关于Spaun的真相-大脑模拟 两个聊天机器人彼此聊天-有趣又怪异 GANPaint:将AI用于艺术 WCF和WF给予社区 从耳朵到脸 所有神经网络的深层缺陷 蠕虫在尾巴上平衡杆子 Kickstarter上的OpenCV AI套件 TensorFlow-Google的开源AI和计算引擎 众包取代新闻工作者 Google的DeepMind学会玩街机游戏 哑机器人V智能机器人 .NET与.NET 5融为一体 Google的深度学习-语音识别 LInQer将.NET LINQ移植到Javascript 机器人TED演讲-新的图灵测试? GAN的发明者加入苹果 您的智能手机会监视您键入的内容 人工智能帮助改善国际象棋 Zalando Flair NLP库已更新 TensorFlow 1.5包含移动版本 AlphaGo输了一场比赛-比分3-1 虚拟机器学习峰会 Microsoft开源AI调试工具 SharePoint走向移动 F#4.0发出文化变革的信号 克里斯蒂拍卖AI艺术品 人工智能如何区分 Facebook在蒙特利尔的新AI实验室 Mozilla想要您的声音 微软使用极深的神经网络赢得ImageNet 建立AI合作伙伴关系 .NET Core 3-Microsoft几乎回到了起点 神经网络-更好的销售商? Google使用AI查找您的住所 虹膜-适用于Android的Siri证明苹果没有优势 TensorFlow 2提供更快的模型训练 深度学习研究人员将为Google工作
您的位置:首页 >前端 >

第十届蓝桥杯省赛java类B组 试题 E:迷宫 (动态规划之回溯法)

问题描述

                                                               试题 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

问题解答

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;}// 回溯:走不通则重置为3map[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;}}

运行结果 

210101010010110010010101100101101001222210001010102000100010000010101001000010000000122112011010010121111011010010001000001101001011100211022222210000212222222010101000110100001010022212131313112010112221111122222010100001001010001212223313110022200011331000113121000010101100011012211313131011112111333110110131210010010010100000012331313311122223331310000010122310011010101011111021133331000211131333111000001213100001100010000001222131331102221331110001101002211100100010010101013121313100011213330001000010012000010100101010111010231310101002213111100100101021001000010000010101012122100100012133000000100000201010110011110100011022221010101223111010101001112000100001100001011001111211010002133310101010100021101010100101000010122222111011121331100000001011200100001011001011010213111000022231331010100100002001010010000100010002313331111213133100101001010121101001010100011010121131112222110131110010100001200011000000101001010222210221110000100000100011002011010110100000010013132102100001110110100101000121000022201110110010110121121010100001001010000100221101212100001000100010212231000101011010000100011201222120001010100101012121111101001000000100101002222110210100101001000021200222222210110100000010011131112210010000111010212112111312000000011010001000100012000000122201112120022211021110101000101000100012221111102212101221210222221200100000101001010010121122222221021012102013111212000011110000100001000222211311100222223122222222121110000001100111010111313331000113111310131131111222--------------------------------------------------DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDDLLDDRRRURRRRRRURURRDDDRRRRUURUUUUUUUULLLUUUURRRRUUULDLLUUUULLUUULUURRDRRUUURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDDDDDDRRRRRRRUULLULDLUUURRRRRRDDDDDDRRU

最终答案

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDDLLDDRRRURRRRRRURURRDDDRRRRUURUUUUUUUULLLUUUURRRRUUULDLLUUUULLUUULUURRDRRUUURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDDDDDDRRRRRRRUULLULDLUUURRRRRRDDDDDDRRU

 

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。