JAVA实现一个迷宫回溯算法-(回溯方法实现)

僤鯓⒐⒋嵵緔 提交于 2019-12-17 00:09:59

回溯的特点

可以用作与不可预测的操作步骤次数但是步骤模式是一样的就是可以使用回溯方法
一个字符匹配组合算法代码
    public static void stringCombinationVersion2(ArrayList<ArrayList<String>> resultList,String [] macthingArr,Stack<Integer> indexStack){
        //判断索引栈是否为空
        if(indexStack==null){
            indexStack=new Stack<>();
        }
        //遍历各个回调方法中的字符数组
        for(int index=0;index<macthingArr.length;index++){

            //判断当前回调中的循环索引是否在其它回调函数层中包含
            if(indexStack.contains(index)) continue;

            //如果没包含就将当前回调方法中的索引存入到索引栈中
            indexStack.push(index);

            //判断当前回调函数层数是否在最后一层
            if(indexStack.size()>=macthingArr.length){

                //代表的当前回调方法层最后一层,将索引栈中的索引取出并组合一组字符串组合
                //创建一个索引数组存储索引栈中的内容,
                int [] indexArray=new int[indexStack.size()];
                int count=0;
                while(indexStack.size()>0){
                    //提示:最靠索引栈栈顶的元素对应都是最后的字符  我们组合的顺序是从前往后组合
                    indexArray[count++]=indexStack.pop();
                }
                //创建一个集合封装对应当前的字符串组合信息
                ArrayList<String>  combinationList=new ArrayList<>();
                
                //循环索引数组
                for(int i=indexArray.length-1;i>=0;i--){
                    combinationList.add(macthingArr[indexArray[i]]);
                    if(i!=0){
                        //除了之前栈顶元素其他元素索引再次存入到索引栈中
                        indexStack.push(indexArray[i]);
                    }
                }
                resultList.add(combinationList);
            }else{
                //使用回调函数将层级走到最后一层为止
                stringCombinationVersion2(resultList,macthingArr,indexStack);

                //回调结束后返回当前层,并将当前层对应的循环索引值更新
                indexStack.pop();
            }
        }
    }

迷宫回溯算法 配合上面的字符串组合

public static void main(String[] args) {

        String [] macthingArray={"上","下","左","右"};
        ArrayList<ArrayList<String>> resultList=new ArrayList<>();

        PracticeStringCombination.stringCombination(resultList,macthingArray,null);
        for(ArrayList<String> arr:resultList){

            //创建一个二维数组(迷宫) 0===未走过  1==围墙    2==已走过   3===走过且思路  4===设置了挡板
            int [][] mazeArray=new int[7][8];

            //设置对应的围墙
            for(int i=0;i<mazeArray.length;i++){
                for(int j=0;j<mazeArray[i].length;j++){

                    if(i>=1&&i<=5){
                        mazeArray[i][0]=1;
                        mazeArray[i][7]=1;
                    }else{
                        mazeArray[i][j]=1;
                        mazeArray[i][j]=1;
                    }
                }
            }

            //设置了挡板
            mazeArray[3][1]=4;
            mazeArray[3][2]=4;
            System.out.println("==============="+arr.toString()+"方向=============");

            mazeBackVersion1(mazeArray,1,1,arr);

            showMaze(mazeArray);
            System.out.println("==============="+arr.toString()+"方向结束=============");
            System.out.println();
        }
    }
    //迷宫回溯版本1
    public static boolean mazeBackVersion1(int [][] mazeArray, int line, int column, ArrayList<String> directionList){

        if(mazeArray[5][6]==2){
            //代表当前重点已经被找到
            return true;
        }
        //表示当前的路径没有走过
        if(mazeArray[line][column]==0){
            //将当前路设置成已走过状态
            mazeArray[line][column]=2;

            //方向1
         	if(mazeBackVersion1(mazeArray,directionLine(directionList.get(0),line),directionColumn(directionList.get(0),column),directionList)){
                return true;
            //方向2 
            }else if(mazeBackVersion1(mazeArray,directionLine(directionList.get(1),line),directionColumn(directionList.get(1),column),directionList)){
                return true;
            //方向3 左
            }else if(mazeBackVersion1(mazeArray,directionLine(directionList.get(2),line),directionColumn(directionList.get(2),column),directionList)){
                return true;
            //方向4 右
            }else if(mazeBackVersion1(mazeArray,directionLine(directionList.get(3),line),directionColumn(directionList.get(3),column),directionList)){
                return true;
            }else{
                System.out.println("当前路行:"+line+"=列:"+column+"走不通");
                //说明当前这个行列的位置是走不通的路设置成3
                mazeArray[line][column]=3;
                return false;
            }
        }else{
            //代表当前的路有可能已经是死路被挡的路所以返回该路
            return  false;

        }
    }
    //方向行的操作
    public static int  directionLine(String directionStr,int lineValue){
        if(directionStr.equals("下")){
            lineValue+=1;

        }else if(directionStr.equals("上")){
            lineValue-=1;
        }
        return  lineValue;
    }

    //方向列的操作
    public static int  directionColumn(String directionStr,int columnValue){
        if(directionStr.equals("右")){
            columnValue+=1;
        }else if(directionStr.equals("左")){
            columnValue-=1;
        }
        return  columnValue;
    }

结果:提示显示一部分在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!