递归求解

递归问题

递归求解全排列

public class Main {
    private static final int maxn=11;
    private static final int n=3;
    private static int[] p = new int[maxn];
    private static boolean[] hashTable = new boolean[maxn];
    private static int count=0;
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        permutations(1);
        System.out.println("数量"+count);
        System.out.println("所需时间:" +String.valueOf(System.currentTimeMillis() - startTime));
    }
//递归求解全排列
    private static void permutations(int index) {
        if (index == n+1) {
                for (int i = 1; i <= n; i++) {
                    System.out.print(p[i]+"  ");
                }
                System.out.println();
                count++;
            return;
        }
        int x = 1;
        for (; x <=n ; ) {
            if (hashTable[x] == false) {
                p[index] = x;
                hashTable[x] = true;
                permutations(index+1);
                hashTable[x] = false;
            }
            x++;
        }
        return;
    }   
}
结果:
1  2  3  
1  3  2  
2  1  3  
2  3  1  
3  1  2  
3  2  1  
数量6
所需时间:1

递归暴力求解N-皇后问题

    public class Main {
    private static final int maxn=11;
    private static final int n=8;
    private static int[] p = new int[maxn];
    private static boolean[] hashTable = new boolean[maxn];
    private static int count=0;
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        violenceNQueen(1);
        System.out.println("数量"+count);
        System.out.println("所需时间:" +String.valueOf(System.currentTimeMillis() - startTime));
    }

    //暴力求解N皇后问题
        private static void violenceNQueen(int index) {
            if (index == n+1) {
                boolean flage = true;
                for(int i = 1; i<=n; i++){
                    int j = i+1;
                    for (; j <= n; j++) {
                        if (Math.abs((i - j)) == Math.abs((p[i] - p[j]))) {
                            flage = false;
                        }
                    }
                }
                if (flage == true) {
                    for (int i = 1; i <= n; i++) {
                        System.out.print(p[i]+"  ");
                    }
                    System.out.println();
                    count++;
                }
                return;
            }
            int x = 1;
            for (; x <=n ; ) {
                if (hashTable[x] == false) {
                    p[index] = x;
                    hashTable[x] = true;
                    violenceNQueen(index+1);
                    hashTable[x] = false;
                }
                x++;
            }
            return;
        }
}
结果
1  5  8  6  3  7  2  4  
1  6  8  3  7  4  2  5  
1  7  4  6  8  2  5  3  
1  7  5  8  2  4  6  3  
2  4  6  8  3  1  7  5  
2  5  7  1  3  8  6  4  
2  5  7  4  1  8  6  3  
2  6  1  7  4  8  3  5  
2  6  8  3  1  4  7  5  
2  7  3  6  8  5  1  4  
2  7  5  8  1  4  6  3  
2  8  6  1  3  5  7  4  
3  1  7  5  8  2  4  6  
3  5  2  8  1  7  4  6  
3  5  2  8  6  4  7  1  
3  5  7  1  4  2  8  6  
3  5  8  4  1  7  2  6  
3  6  2  5  8  1  7  4  
3  6  2  7  1  4  8  5  
3  6  2  7  5  1  8  4  
3  6  4  1  8  5  7  2  
3  6  4  2  8  5  7  1  
3  6  8  1  4  7  5  2  
3  6  8  1  5  7  2  4  
3  6  8  2  4  1  7  5  
3  7  2  8  5  1  4  6  
3  7  2  8  6  4  1  5  
3  8  4  7  1  6  2  5  
4  1  5  8  2  7  3  6  
4  1  5  8  6  3  7  2  
4  2  5  8  6  1  3  7  
4  2  7  3  6  8  1  5  
4  2  7  3  6  8  5  1  
4  2  7  5  1  8  6  3  
4  2  8  5  7  1  3  6  
4  2  8  6  1  3  5  7  
4  6  1  5  2  8  3  7  
4  6  8  2  7  1  3  5  
4  6  8  3  1  7  5  2  
4  7  1  8  5  2  6  3  
4  7  3  8  2  5  1  6  
4  7  5  2  6  1  3  8  
4  7  5  3  1  6  8  2  
4  8  1  3  6  2  7  5  
4  8  1  5  7  2  6  3  
4  8  5  3  1  7  2  6  
5  1  4  6  8  2  7  3  
5  1  8  4  2  7  3  6  
5  1  8  6  3  7  2  4  
5  2  4  6  8  3  1  7  
5  2  4  7  3  8  6  1  
5  2  6  1  7  4  8  3  
5  2  8  1  4  7  3  6  
5  3  1  6  8  2  4  7  
5  3  1  7  2  8  6  4  
5  3  8  4  7  1  6  2  
5  7  1  3  8  6  4  2  
5  7  1  4  2  8  6  3  
5  7  2  4  8  1  3  6  
5  7  2  6  3  1  4  8  
5  7  2  6  3  1  8  4  
5  7  4  1  3  8  6  2  
5  8  4  1  3  6  2  7  
5  8  4  1  7  2  6  3  
6  1  5  2  8  3  7  4  
6  2  7  1  3  5  8  4  
6  2  7  1  4  8  5  3  
6  3  1  7  5  8  2  4  
6  3  1  8  4  2  7  5  
6  3  1  8  5  2  4  7  
6  3  5  7  1  4  2  8  
6  3  5  8  1  4  2  7  
6  3  7  2  4  8  1  5  
6  3  7  2  8  5  1  4  
6  3  7  4  1  8  2  5  
6  4  1  5  8  2  7  3  
6  4  2  8  5  7  1  3  
6  4  7  1  3  5  2  8  
6  4  7  1  8  2  5  3  
6  8  2  4  1  7  5  3  
7  1  3  8  6  4  2  5  
7  2  4  1  8  5  3  6  
7  2  6  3  1  4  8  5  
7  3  1  6  8  5  2  4  
7  3  8  2  5  1  6  4  
7  4  2  5  8  1  3  6  
7  4  2  8  6  1  3  5  
7  5  3  1  6  8  2  4  
8  2  4  1  7  5  3  6  
8  2  5  3  1  7  4  6  
8  3  1  6  2  5  7  4  
8  4  1  3  6  2  7  5  
数量92
所需时间:70

递归回溯求解N-皇后问题
public class Main {
    private static final int maxn=11;
    private static final int n=8;
    private static int[] p = new int[maxn];
    private static boolean[] hashTable = new boolean[maxn];
    private static int count=0;
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        backTrackNQueen(1);
        System.out.println("数量"+count);
        System.out.println("所需时间:" +String.valueOf(System.currentTimeMillis() - startTime));
    }

    //回溯求解N皇后问题 
    private static void backTrackNQueen(int index) {
            if (index == n+1) {
                count++;
                for (int i = 1; i <= n; i++) {
                    System.out.print(p[i]+"  ");
                }
                System.out.println();
                return;
            }

            for (int x = 1; x <= n; x++) {
                if (hashTable[x] == false) {
                    boolean flag = true;
                    for (int pre = 1; pre < index; pre++) {
                        if (Math.abs((index - pre)) == Math.abs((x - p[pre]))) {
                            flag = false;
                            break;
                        }
                    }//end pre
                    if (flag) {
                        p[index] = x;
                        hashTable[x] = true;
                        backTrackNQueen(index+1);
                        hashTable[x] = false;
                    }
                }
            }
        }
}
结果
1  5  8  6  3  7  2  4  
1  6  8  3  7  4  2  5  
1  7  4  6  8  2  5  3  
1  7  5  8  2  4  6  3  
2  4  6  8  3  1  7  5  
2  5  7  1  3  8  6  4  
2  5  7  4  1  8  6  3  
2  6  1  7  4  8  3  5  
2  6  8  3  1  4  7  5  
2  7  3  6  8  5  1  4  
2  7  5  8  1  4  6  3  
2  8  6  1  3  5  7  4  
3  1  7  5  8  2  4  6  
3  5  2  8  1  7  4  6  
3  5  2  8  6  4  7  1  
3  5  7  1  4  2  8  6  
3  5  8  4  1  7  2  6  
3  6  2  5  8  1  7  4  
3  6  2  7  1  4  8  5  
3  6  2  7  5  1  8  4  
3  6  4  1  8  5  7  2  
3  6  4  2  8  5  7  1  
3  6  8  1  4  7  5  2  
3  6  8  1  5  7  2  4  
3  6  8  2  4  1  7  5  
3  7  2  8  5  1  4  6  
3  7  2  8  6  4  1  5  
3  8  4  7  1  6  2  5  
4  1  5  8  2  7  3  6  
4  1  5  8  6  3  7  2  
4  2  5  8  6  1  3  7  
4  2  7  3  6  8  1  5  
4  2  7  3  6  8  5  1  
4  2  7  5  1  8  6  3  
4  2  8  5  7  1  3  6  
4  2  8  6  1  3  5  7  
4  6  1  5  2  8  3  7  
4  6  8  2  7  1  3  5  
4  6  8  3  1  7  5  2  
4  7  1  8  5  2  6  3  
4  7  3  8  2  5  1  6  
4  7  5  2  6  1  3  8  
4  7  5  3  1  6  8  2  
4  8  1  3  6  2  7  5  
4  8  1  5  7  2  6  3  
4  8  5  3  1  7  2  6  
5  1  4  6  8  2  7  3  
5  1  8  4  2  7  3  6  
5  1  8  6  3  7  2  4  
5  2  4  6  8  3  1  7  
5  2  4  7  3  8  6  1  
5  2  6  1  7  4  8  3  
5  2  8  1  4  7  3  6  
5  3  1  6  8  2  4  7  
5  3  1  7  2  8  6  4  
5  3  8  4  7  1  6  2  
5  7  1  3  8  6  4  2  
5  7  1  4  2  8  6  3  
5  7  2  4  8  1  3  6  
5  7  2  6  3  1  4  8  
5  7  2  6  3  1  8  4  
5  7  4  1  3  8  6  2  
5  8  4  1  3  6  2  7  
5  8  4  1  7  2  6  3  
6  1  5  2  8  3  7  4  
6  2  7  1  3  5  8  4  
6  2  7  1  4  8  5  3  
6  3  1  7  5  8  2  4  
6  3  1  8  4  2  7  5  
6  3  1  8  5  2  4  7  
6  3  5  7  1  4  2  8  
6  3  5  8  1  4  2  7  
6  3  7  2  4  8  1  5  
6  3  7  2  8  5  1  4  
6  3  7  4  1  8  2  5  
6  4  1  5  8  2  7  3  
6  4  2  8  5  7  1  3  
6  4  7  1  3  5  2  8  
6  4  7  1  8  2  5  3  
6  8  2  4  1  7  5  3  
7  1  3  8  6  4  2  5  
7  2  4  1  8  5  3  6  
7  2  6  3  1  4  8  5  
7  3  1  6  8  5  2  4  
7  3  8  2  5  1  6  4  
7  4  2  5  8  1  3  6  
7  4  2  8  6  1  3  5  
7  5  3  1  6  8  2  4  
8  2  4  1  7  5  3  6  
8  2  5  3  1  7  4  6  
8  3  1  6  2  5  7  4  
8  4  1  3  6  2  7  5  
数量92
所需时间:28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值