五大基本算法思想之递归回溯

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_29857681/article/details/95755908

使用场景: 求解所有可行方案。 整个过程有多个步骤,每个步骤有多个选择,从中选出可行方案

八皇后

在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法

public class EightQueen{
    public static int[][] arry=new int[8][8];//棋盘,放皇后
    public static int map=0;//存储方案结果数量

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("八皇后问题");
        findQueen(0);
        System.out.println("八皇后问题共有:"+map+"种可能");
    }

    public static void findQueen(int i){//寻找皇后节点
        if(i>7){//八皇后的解
            map++;
//            print();//打印八皇后的解
            return;
        }

        for(int m=0;m<8;m++){//深度回溯,递归算法
            if(check(i,m)){//检查皇后摆放是否合适
                arry[i][m]=1;
                findQueen(i+1);
                arry[i][m]=0;//清零,以免回溯的时候出现脏数据
            }
        }
    }

    public static boolean check(int k,int j){//判断节点是否合适
        for(int i=0;i<8;i++){//检查行列冲突
            if(arry[i][j]==1){
                return false;
            }
        }
        for(int i=k-1,m=j-1; i>=0 && m>=0; i--,m--){//检查左对角线
            if(arry[i][m]==1){
                return false;
            }
        }
        for(int i=k-1,m=j+1; i>=0 && m<=7; i--,m++){//检查右对角线
            if(arry[i][m]==1){
                return false;
            }
        }
        return true;
    }

    public static void print(){//打印结果
        System.out.print("方案"+map+":"+"\n");
        for(int i=0;i<8;i++){
            for(int m=0;m<8;m++){
                if(arry[i][m]==1){
                    //System.out.print("皇后"+(i+1)+"在第"+i+"行,第"+m+"列\t");
                    System.out.print("o ");
                }
                else{
                    System.out.print("+ ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }
}

机器人行动范围(引自:https://www.cnblogs.com/DarrenChan/p/10247329.html

地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

public class Solution {
    public static void main(String[] args) {
        System.out.println(movingCount(18, 56, 56));
    }
     public static int movingCount(int threshold, int rows, int cols) {
        boolean[][] flag = new boolean[rows][cols];
        return movingCount(threshold, rows, cols, 0, 0, flag);
    }

    private static int movingCount(int threshold, int rows, int cols, int i, int j, boolean[][] flag){
        if(i < 0 || i >= rows || j < 0 || j >= cols || getSum(i) + getSum(j) > threshold || flag[i][j] == true){
            return 0;
        }
        flag[i][j] = true;
        return movingCount(threshold, rows, cols, i - 1, j, flag) +
                movingCount(threshold, rows, cols, i + 1, j, flag) +
                movingCount(threshold, rows, cols, i, j - 1, flag) +
                movingCount(threshold, rows, cols, i, j + 1, flag) + 1;
    }

    private static int getSum(int num){
        if(num == 0){
            return 0;
        }else{
            return num % 10 + getSum(num / 10);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_29857681/article/details/95755908