文章图片
原文件内容:
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
【程序设计算法|2019年第十一届蓝桥杯省赛JavaB组第E题——“迷宫“题目及解析】解析:这道题看到30行50列的大小,我们就应该舍弃dfs的做法,因为超时是在所难免的,所以看到最短路,很自然的想到BFS,我们只需要记录一下每次走的方向即可,难度中等
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class 迷宫 {
static boolean [][]key=new boolean[31][51];
static int [][]move={{1,0},{0,-1},{0,1},{-1,0}};
static char [] moves={'D','L','R','U'};
//按照字典序最小来进行移动
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("D:\\360\\mrked.txt"));
String t="";
String [] arr=new String[31];
int cur=1;
while((t=reader.readLine())!=null) {
arr[cur++]=t;
}
char [][]arrs=new char[31][];
for (int i = 1;
i < arrs.length;
i++) {
arrs[i]=new char[51];
for (int j = 1;
j < arrs[i].length;
j++) {
arrs[i][j]=arr[i].charAt(j-1);
}
}
LinkedList queue = new LinkedList<>();
queue.add(new dian(1,1,""));
key[1][1]=true;
String ans="";
while(!queue.isEmpty())
{
dian s1=queue.poll();
int x1=s1.x;
int y1=s1.y;
String str1=s1.stepchr;
if(x1==30&&y1==50)
{
ans=str1;
break;
}
for (int i = 0;
i < moves.length;
i++) {
int x2=x1+move[i][0];
int y2=y1+move[i][1];
if(x2>=1&&x2<=30&&y2>=1&&y2<=50&&arrs[x2][y2]!='1'&&!key[x2][y2])
{
queue.add(new dian(x2,y2,str1+moves[i]));
key[x2][y2]=true;
}
}
}
System.out.println(ans);
}
}
class dian
{
int x;
int y;
public dian(int x, int y, String stepchr) {
this.x = x;
this.y = y;
this.stepchr = stepchr;
}String stepchr;
}
答案:
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
推荐阅读
- 酷划锁屏手机赚钱APP的使用经验分享
- android 字符串
- CTF训练|【picoCTF2022】Misc部分
- Android关于编译ijkplayer下的so经验分享
- 量化字符串(安卓权威编程 13.7挑战练习-优化字符串资源显示)
- 经验分享|【经验分享】GPU CUDA 使用 memory padding 避免 bank conflict
- 计算机考研复试面试|计算机考研复试面试常问问题 操作系统篇
- 微电子专业|微电子专业是做芯片的吗(芯片和什么专业有关?)
- 经验建议|硕士阶段个人经验