zoukankan      html  css  js  c++  java
  • 递归

    递归

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

    • 递归用于解决什么样的问题
    • 各种数学问题如: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);
                }
            }
        }
    }
    
    
  • 相关阅读:
    (三) 权限控制 --《springboot与shiro整合》
    (二) shiro集成 --《springboot与shiro整合》
    (一) springboot 项目搭建 --《springboot与shiro整合》
    第四章 编码/加密(学习笔记)
    第三章 授权(学习笔记)
    第二章 身份验证(学习笔记)
    获取小程序码java实现
    paypal退款 java实现
    并发下的数据处理和优化
    Casperjs初体验
  • 原文地址:https://www.cnblogs.com/chaostudy/p/12979671.html
Copyright © 2011-2022 走看看