JAVA实现一个迷宫回溯-(使用回溯加双端算法)

回溯的特点
可以用作与不可预测的操作步骤次数但是步骤模式是一样的就是可以使用回溯方法
一个字符匹配组合算法代码 思路:使用双端队列及回溯实现
    //编写一个字符组合方式方法
    public static List<List<String>> stringGroupWay(String [] stringArr){

        //创建一个封装每组组合好的结果集集合
        List<List<String>> resultList=new ArrayList<>();
        //创建一个封装每一层回溯对应的stringArr索引
        Deque<Integer> layerIndexs=new ConcurrentLinkedDeque<>();
        //调用字符串组合方法
        stringGroupWay(stringArr,resultList,layerIndexs);
        return resultList;
    }
    private static void stringGroupWay(String [] stringArr, List<List<String>> resultList, Deque<Integer>layerIndexs){

        //循环遍历当前层对应的stringArr
        for(int index=0;index<stringArr.length;index++){

            if(layerIndexs.contains(index))continue;

            //将当前层对应的索引存储到layerIndexs
            layerIndexs.addLast(index);

            //判断是否已经回溯到最后一层
            if(layerIndexs.size()>=stringArr.length){

                //创建一个封装当前层对应的一组组合内容集合
                List<String> groupList=new ArrayList<>();

                //创建一个存储每一层索引的数组
                Integer [] layerArr=new Integer[layerIndexs.size()];
                layerArr= Arrays.copyOfRange(layerIndexs.toArray(),0,layerIndexs.size(),Integer[].class);

                for(int currIndex=0;currIndex<layerArr.length;currIndex++){
                    groupList.add(stringArr[layerArr[currIndex]]);
                }
                resultList.add(groupList);
            }else{
                //没有回溯到最后一层 就继续回溯
                stringGroupWay(stringArr,resultList,layerIndexs);
            }
            //将当前层对应的索引移除
            layerIndexs.removeLast();
        }
    }
迷宫回溯算法 配合上面的字符串组合
//迷宫回溯演示类
public class MazeBackDemo {
    public static void main(String[] args) {
        String[] arr={"上","下","左","右"};
        List<List<String>> lists = MazeBack.stringGroupWay(arr);
        for(List<String> arrL:lists){
            //创建一个迷宫
            int [][] maze=new int[7][8];
            //设置围墙
            maze[2][1]=3;
            maze[2][2]=3;
            maze[2][3]=3;
            maze[2][4]=3;
            maze[2][5]=3;
            maze[2][6]=3;
            maze[2][7]=3;
            boolean bol=MazeBack.mazeBack(maze,1,1,arrL);
            System.out.println("=========="+arrL.toString()+"===迷宫开始======="+(bol?"成功":"失败"));
            for(int [] arr2:maze){
                for(int arr3:arr2){
                    System.out.print(arr3+"\t");
                }
                System.out.println();
            }

            System.out.println("=========="+arrL.toString()+"===迷宫结束=======");
            System.out.println();
            System.out.println();
        }
    }
}

//迷宫回溯类
class MazeBack{

    private static int countNumber=0;
    //编写一个字符组合方式方法
    public static List<List<String>> stringGroupWay(String [] stringArr){

        //创建一个封装每组组合好的结果集集合
        List<List<String>> resultList=new ArrayList<>();
        //创建一个封装每一层回溯对应的stringArr索引
        Deque<Integer> layerIndexs=new ConcurrentLinkedDeque<>();
        //调用字符串组合方法
        stringGroupWay(stringArr,resultList,layerIndexs);
        return resultList;
    }
    private static void stringGroupWay(String [] stringArr, List<List<String>> resultList, Deque<Integer>layerIndexs){

        //循环遍历当前层对应的stringArr
        for(int index=0;index<stringArr.length;index++){

            if(layerIndexs.contains(index))continue;

            //将当前层对应的索引存储到layerIndexs
            layerIndexs.addLast(index);

            //判断是否已经回溯到最后一层
            if(layerIndexs.size()>=stringArr.length){

                //创建一个封装当前层对应的一组组合内容集合
                List<String> groupList=new ArrayList<>();

                //创建一个存储每一层索引的数组
                Integer [] layerArr=new Integer[layerIndexs.size()];
                layerArr= Arrays.copyOfRange(layerIndexs.toArray(),0,layerIndexs.size(),Integer[].class);

                for(int currIndex=0;currIndex<layerArr.length;currIndex++){
                    groupList.add(stringArr[layerArr[currIndex]]);
                }
                resultList.add(groupList);
            }else{
                //没有回溯到最后一层 就继续回溯
                stringGroupWay(stringArr,resultList,layerIndexs);
            }
            //将当前层对应的索引移除
            layerIndexs.removeLast();
        }
    }
    //迷宫回溯方法
    public static boolean mazeBack(int [][] maze,int line,int column,List<String> directions ){
        countNumber++;
        if(maze[5][7]==2){
            System.out.println("次数:"+countNumber);
            countNumber=0;
            return true;  //说明已经找到了终点位置
        }
        //不能超出迷宫范围
        if(line<0||line>maze.length-1||column<0||column>maze[0].length-1) return false;
        if(maze[line][column]==0){
            //由于当前位置没有走过所以将当前位置设置已走过
            maze[line][column]=2;
            //创建四个方向位置
            if(mazeBack(maze,lineCalculate(line,directions.get(0)),columnCalculate(column,directions.get(0)),directions)){
                return true;
            }
            else if(mazeBack(maze,lineCalculate(line,directions.get(1)),columnCalculate(column,directions.get(1)),directions)){
                return true;
            }
            else if(mazeBack(maze,lineCalculate(line,directions.get(2)),columnCalculate(column,directions.get(2)),directions)){
                return true;
            }
            else if(mazeBack(maze,lineCalculate(line,directions.get(3)),columnCalculate(column,directions.get(3)),directions)){
                return true;
            }else{
                //由于当前的路的四个方向位置都是走不通的路(有可能能路已经是 2 或 3) 所以将当前路设置为死路 3
                maze[line][column]=3;
                return false;
            }
        }else{
            return false; //代表当前的路已经走过 ==2 获取当前的路之前走过并且是死路 ==3;
        }

    }
    //行的计算
    private static int lineCalculate(int line,String direction){
        if(direction.equals("上")){
            line-=1;
        }else if(direction.equals("下")){
            line+=1;
        }
        return line;
    }
    //列的计算
    private static int columnCalculate(int column,String direction){

        if(direction.equals("左")){
            column-=1;
        }else if(direction.equals("右")){
            column+=1;
        }
        return column;
    }
}

结果:提示显示一部分

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值