Recursive application (maze problem and eight queens problem)

Maze problem

右下角位置为出路
Insert picture description here

package com.whb.recursion;

public class MiGong {
    
    

    public static void main(String[] args) {
    
    

        // 创建二维数组, 模拟迷宫
        // 地图
        int[][] map = new int[8][7];
        // 1 表示墙
        for (int i = 0; i < 7; i++) {
    
    
            map[0][i] = 1;
            map[7][i] = 1;
        }

        for (int i = 0; i < 8; i++) {
    
    
            map[i][0] = 1;
            map[i][6] = 1;
        }

        map[3][1] = 1;
        map[3][2] = 1;

        System.out.println("map...");
        for (int i = 0; i < 8; i++) {
    
    
            for (int j = 0; j < 7; j++) {
    
    
                System.out.println(map[i][j] + " ");
            }
            System.out.println();
        }

        setWay(map, 1, 1);

        System.out.println("小球走过,并标识过的 地图的情况");
        for (int i = 0; i < 8; i++) {
    
    
            for (int j = 0; j < 7; j++) {
    
    
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }

    // 1. map 表示地图
    // 2. i,j 表示当前位置坐标
    // 3. 如果小球能到 map[6][5] 位置,则说明通路找到.
    // 4. 约定: 当map[i][j] 为 0 表示该点没有走过 当为 1 表示墙; 2 表示通路可以走; 3 表示该点已经走过,但是走不通
    // 5. 在走迷宫时,需要确定一个策略(方法) 下->右->上->左 , 如果该点走不通,再回溯
    public static boolean setWay(int[][] map, int i, int j){
    
    
        if(map[6][5] == 2){
    
    
            return true;
        } else {
    
    
            // 如果这个点还没走过
            if (map[i][j] == 0){
    
    
                // 下->右->上->左
                map[i][j] = 2;
                if(setWay(map, i+1, j)){
    
    
                    return true;
                }else if(setWay(map, i, j+1)){
    
    
                    return true;
                }else if (setWay(map, i-1, j)){
    
    
                    return true;
                }else if (setWay(map, i, j-1)){
    
    
                    return true;
                }else{
    
    
                    map[i][j] = 3; // 说明该点是走不通,是死路
                    return false;
                }

            }else{
    
     // 如果map[i][j] != 0 , 可能是 1, 2, 3
                return false;
            }
        }
    }
}

Eight Queens Problem

Introduction to the Eight Queens Problem

The eight queens problem is an old and famous problem, a typical case of backtracking algorithms. The problem was raised by the chess player Max Bethel in 1848: place eight queens on an 8×8 chess board so that they cannot attack each other, that is: no two queens can be in the same row , In the same column or on the same diagonal line, ask how many ways there are.

思路分析
Insert picture description here
代码实现

package com.whb.recursion;

public class Queen8 {
    
    

    private static int max = 8;
    // 下标代表第几行,或者代表第几个皇后, val代表第几列
    private static int []arr = new int[max];
    private static int corrCount;

    public static void main(String[] args) {
    
    

        check(0);
        System.out.printf("有%d种解法\n", corrCount);
    }

    /**
     *
     * @param n 当前在第几行(或第几个皇后)
     * @return
     */
    public static boolean judge(int n){
    
    
        // 判断跟前面摆放好的皇后是否有冲突
        for (int i = 0; i < n; i++) {
    
    
            // 在同一列 在 一条斜线(直角三角形)行和列的差值一样
            if (arr[n]==arr[i] || (n-i == Math.abs(arr[n] - arr[i]))){
    
    
                return false;
            }
        }
        return true;
    }

    /**
     *
     * @param n 当前到第几个皇后了
     * @return
     */
    public static void check(int n){
    
    
        // 表示到第max+1个皇后了,说明前max个皇后(即所有列)摆放都符合要求
        if (n==max){
    
    
            print();
            return;
        }

        // 遍历这一行的每一个位置
        // 当前行遍历完后,有回溯
        for (int i = 0; i < max; i++) {
    
    
            // 当前位置
            arr[n] = i;
            // 如果这一行(当前的n行) 位置与前面摆放的所有皇后不冲突,则继续向下一行摆放
            if (judge(n)){
    
    
                check(n+1);
            }
        }
    }

    // 打印
    public static void print(){
    
    
        corrCount++;
        for (int i = 0; i < max; i++) {
    
    
            // (行坐标, 列坐标)
            System.out.printf("(%d, %d)\t", i+1, arr[i]+1);
        }
        System.out.println();
    }
}

Guess you like

Origin blog.csdn.net/qq_46456049/article/details/112858313