算法02-回溯算法

算法02-回溯算法

1.回溯算法-八皇后

/**
 * row and col not cat save more queen
 */
public class EightQueen {

    private final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

    public void calculate(int queen, int y, int[] checkerboard) {
        if (queen == y)
            threadLocal.set(threadLocal.get() + 1);
        else {
            for (int i = 0;i < queen;i++) {
                // find can save queen place
                if (checkerboard[i] == 0) {

                    checkerboard[i] = 1;
                    calculate(queen, y + 1, checkerboard);
                    // back track and find next can save queen place
                    // if(i==queen) again back track
                    checkerboard[i] = 0;
                }
            }
        }
    }

    /**
     * queen=1 1
     * queen=2 queen=1*2=2
     * queen=3 queen(2)=2*3-6
     * queen=4 queen(3)=6*4=24
     * @param queen
     * @return
     */
    public int simpleCalculate(int queen) {
        if (queen == 1)
            return 1;

        return simpleCalculate(queen - 1) * queen;
    }

    public int calculate(int queen) {
        threadLocal.set(0);
        calculate(queen, 0, new int[queen]);
        return threadLocal.get();
    }


    public static void main(String[] args) {
        EightQueen queen = new EightQueen();
        // queen size
        int queenSize = 4;
        //System.out.println(queen.calculate(queenSize));
        System.out.println(queen.simpleCalculate(queenSize));
    }
}

2.回溯算法-马踏棋盘

/**
 * 0 left top
 * 1 right top
 * 2 right bottom
 * 3 left bottom
 */
public class HorseRidingChessboard {

    private static final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

    private final int[] arrX = {-1, 1, 1, -1};
    private final int[] arrY = {-1, -1, 1 ,1};

    public boolean horseCanGo(int chessboardSize, int x, int y, int[][] chessboard) {
        if (x < 0 || y < 0)
            return false;

        if (x >= chessboardSize || y >= chessboardSize)
            return false;

        // not can go place that use 1 mark
        return chessboard[x][y] == 0;
    }

    public void calculateHorseRoute(int chessboardSize, int x, int y, int[][] chessboard) {
        if (x == chessboardSize - 1 && y == chessboardSize - 1)
            // horse reaches the end
            threadLocal.set(threadLocal.get() + 1);
        else {
            // traverse four direction
            for (int i = 0;i < 4;i++) {
                x += arrX[i];
                y += arrY[i];

                if (horseCanGo(chessboardSize, x, y, chessboard)) {
                    // can go and mark
                    chessboard[x][y] = 1;
                    calculateHorseRoute(chessboardSize, x, y, chessboard);
                    // revoke mark
                    chessboard[x][y] = 0;
                }

                // back tracking
                x -= arrX[i];
                y -= arrY[i];
            }
        }
    }

    public int calculateHorseRoute(int chessboardSize) {
        threadLocal.set(0);
        int[][] chessboard = new int[chessboardSize][chessboardSize];
        // init chessboard, avoid horse again go {0,0}
        chessboard[0][0] = 1;

        calculateHorseRoute(chessboardSize, 0, 0, chessboard);
        return threadLocal.get();
    }

    public static void main(String[] args) {
        // chessboard size
        final int chessboardSize = 4;
        HorseRidingChessboard horseRidingChessboard = new HorseRidingChessboard();
        int result = horseRidingChessboard.calculateHorseRoute(chessboardSize);

        System.out.println(result);
    }
}
posted @ 2022-05-28 16:02  行稳致远方  阅读(11)  评论(0)    收藏  举报