【问题描述】
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可
以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这
个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫,
一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。
对于下面这个更复杂的迷宫(30 行 50 列) ,请找出一种通过迷宫的方式,
其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。
请注意在字典序中D<L<R<U。(如果你把以下文字复制到文本文件中,请务
必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 maze.txt,
内容与下面的文本相同)
========================
(maze.txt 就不放上来了,看着乱)
思路:
题目所求:最短路径前提下最小字典序
最短路径->bfs->两个队列
最小字典序->保存前一次的最小字母序的字符串->map<integer ,string>
心得:
对于这类思路简单但是逻辑判断复杂的题目,在写的同时加入简单的注释是十分有必要的
并且,最好一步一调试
public class main_g {
static char[] a;
static Queue<Integer> q =new LinkedList<Integer>();
static Queue<Integer> tmp_q =new LinkedList<Integer>();
static Map<Integer ,String> map =new HashMap<Integer ,String>();
public static void main(String[] args) throws FileNotFoundException {
init();
long l =System.nanoTime(); //开始时间
//调试
// for(int i =0 ;i <30 ;i ++) {for(int j =0 ;j <50 ;j ++) System.out.print(a[i*50 +j]); System.out.println();}
//赋初始值
q.add(0);
map.put(0 ,"");
//临时变量
int[] index_oper = {-1 ,1 ,-50 ,50};
char[] path_oper = {'L' ,'R' ,'U' ,'D'};
boolean isEnd =false;
int index =0 ,tmp_index =0;
String path ="" ,tmp_path ="";
while(true) {
if(q.isEmpty()) {
//函数出口
if(isEnd) break;
//题目隐藏条件:必有结果
while(!tmp_q.isEmpty()) q.add(tmp_q.poll());
}
// System.out.println(index);
index =q.poll();
path =map.get(index);
for(int i =0 ;i <index_oper.length ;i ++) {
tmp_index =index +index_oper[i];
tmp_path =path +path_oper[i];
// System.out.println(tmp_index +" "+tmp_path); //调试
//边界判断
if(tmp_index <0 ||tmp_index >=30 *50) continue;
if(index_oper[i] ==-1 &&index %50 ==0) continue;
if(index_oper[i] ==1 &&(index +1) %50 ==0) continue;
//得是条路
if(a[tmp_index] =='1') continue;
//重复判断
if(tmp_path.endsWith("LR") ||tmp_path.endsWith("RL")) continue;
if(tmp_path.endsWith("UD") ||tmp_path.endsWith("DU")) continue;
//之前已保存有值
if(map.containsKey(tmp_index)) {
if(count(map.get(tmp_index)) >=count(tmp_path)) continue;
}
//添加新值
tmp_q.add(tmp_index);
map.put(tmp_index, tmp_path);
//第一次到终点,触发退出
if(tmp_index ==30 *50 -1) isEnd =true;
}
}
//输出结果
System.out.println(map.get(30 *50 -1));
//输出一下用时
System.out.println("用时"+(System.nanoTime() -l)); //用时139479600
}
private static int count(String tmp_path) {
int ans =0;
char[] c =tmp_path.toCharArray();
for(char tmp :c) if(tmp =='L') ans ++ ; else if(tmp =='R') ans +=2; else if(tmp =='U') ans +=3;
return ans;
}
private static void init() throws FileNotFoundException {
Scanner sc = new Scanner(new File("src/cn/蓝桥/省赛19/maze.txt"));
String tmp ="";
while(sc.hasNext()) tmp +=sc.next();
sc.close();
a =tmp.toCharArray();
}
}
改进:
利用map中最后一个节点(30 *50 -1)的存在与否->是否为最后一次遍历(代替使用标志位),并解决了反复横跳的问题
public class Main{
static char[] a;
static Queue<Integer> q =new LinkedList<Integer>() ,tmp_q =new LinkedList<Integer>();
static Map<Integer ,String> map =new HashMap<Integer ,String>();
public static void main(String[] args) throws FileNotFoundException {
init();
map.put(0, "");
q.add(0);
int[] oper = {-1 ,1 ,-50 ,50};
int index =0 ,tmp_index =0;
String path ="" ,tmp_path ="";
while(true) {
if(q.isEmpty()) {
if(map.containsKey(30 *50 -1)) break; //最后一次遍历
while(!tmp_q.isEmpty()) q.add(tmp_q.poll());
}
//遍历单位
index =q.poll();
path =map.get(index);
for(int o :oper) {
//四个方向的邻点
tmp_index =index +o;
tmp_path =path;
//去重
if(map.containsKey(tmp_index) &&tmp_index !=30 *50 -1) continue;
//垂直越界
if(tmp_index <0 ||tmp_index >=30 *50) continue;
//水平越界
if(index %50 ==0 &&o ==-1) continue;
if((index +1) %50 ==0 &&o ==1) continue;
//撞墙
if(a[tmp_index] =='1') continue;
//生成新路径
if(o ==-1) tmp_path +="L"; else if(o ==1) tmp_path +="R"; else if(o ==50) tmp_path +="D"; else tmp_path +="U";
//最后一次遍历,找最小字母序
if(index ==30 *50 -1) if(!replace(tmp_path)) continue;
tmp_q.add(tmp_index);
map.put(tmp_index, tmp_path);
}
}
System.out.println(map.get(30 *50 -1));
}
//是否需要取代最小字典序串
private static boolean replace(String s) {
char[] path =map.get(30 *50 -1).toCharArray() ,tmp_path =s.toCharArray();
int sum =0 ,tmp_sum =0;
for(int i =0 ;i <path.length ;i ++) {
if(path[i] =='L') sum +=1; else if(path[i] =='R') sum +=2; else if(path[i] =='U') sum +=3;
if(tmp_path[i] =='L') sum +=1; else if(tmp_path[i] =='R') sum +=2; else if(tmp_path[i] =='U') sum +=3;
}
if(sum >tmp_sum) return true; else return false; //有解->不会出现"="
}
private static void init() throws FileNotFoundException {
Scanner sc = new Scanner(new File("src/cn/test/maze.txt"));
String s ="";
while(sc.hasNext()) s +=sc.next();
a =s.toCharArray();
sc.close();
}
}
解果:
DDDDRRURRRRRRRDRRRDDDLDDRDDDDDDDDDDDDRDRDRRURRUURRDDDDRDRRRRRRURRDRRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDRDRRRRDRDRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR