递归

时间:2020-05-28
本文章向大家介绍递归,主要包括递归使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

递归

递归能解决什么样的问题.

  • 递归用于解决什么样的问题
  • 各种数学问题如:8皇后问题,汉诺塔,阶乘问题,迷宫问题,球和篮子的问题(google编程大赛)
  • 各种算法中也会使用到递归,比如快排,归并排序,二分查找,分治算法等.)将用栈解决的问题-->第归代码比较简洁

迷宫问题

public class MiGong {
    public static void main(String[] args) {
        //构建迷宫
        int map[][] = new int[8][7];
        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;
        map[5][2] = 1;
        map[5][3] = 1;
        map[5][4] = 1;
        map[5][5] = 1;
        //查看迷宫
        for (int i = 0; i <8 ; i++) {
            for(int j = 0; j < 7; j++){
                System.out.print(map[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println();
        setWay(map,1,1);
        for (int i = 0; i <8 ; i++) {
            for(int j = 0; j < 7; j++){
                System.out.print(map[i][j]+" ");
            }
            System.out.println();
        }
    }
    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 {
                return false;        //1,2,3,时返回false
            }
        }
    }
}

八皇后问题

public class Huanghou8 {

    int max = 8;
    int[] array =new int[max];
    static int count = 0;
    public static void main(String[] args) {
        Huanghou8 huanghou8 = new Huanghou8();
        huanghou8.check(0);
        System.out.println(count);
    }
    //打印结果
    private void print(){
        count++;
        for (int i = 0; i <array.length ; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
    //判断是否冲突
    public boolean judge(int n){
        for (int i = 0; i < n; i++) {
            if (array[i] == array[n] || Math.abs(n-i) == Math.abs(array[n]-array[i])){
                return false;
            }
        }
        return true;
    }
    //放入棋子,然后递归
    private void check(int n){
        if (n == max){
            print();
            return;
        }
        for (int i = 0; i < max; i++) {
            array[n] = i;
            if (judge(n)){
                check(n+1);
            }
        }
    }
}

原文地址:https://www.cnblogs.com/chaostudy/p/12979671.html