美文网首页
n皇后问题的解法

n皇后问题的解法

作者: xiaohei_e853 | 来源:发表于2022-03-15 11:39 被阅读0次

    解法一

    import java.util.Scanner;
    
    public class Demo {
        static int[] q = new int[20];
        static int count = 0;
    
        public static void n_queens(int k, int n) {
            int j;
            if (k > n)
                print(n);
            else {
                for (j = 1; j <= n; j++) // 试探第k行的每一列,找到符合要求的列
                {
                    if (isSafe(k, j)) {
                        q[k] = j;
                        n_queens(k + 1, n); // 在确认第 k 行皇后位置的前提下,继续测试下一行皇后的位置
                    }
                }
            }
        }
    
        public static boolean isSafe(int k, int j) {
            int i;
            for (i = 1; i < k; i++) {
                // 如果有其它皇后位置同一列上,或者位于该位置的斜线位置上,则该位置无法使用
                if (q[i] == j || Math.abs(i - k) == Math.abs(q[i] - j))
                    return false;
            }
            return true;
        }
    
        // 输出 N 皇后问题的解决方案
        public static void print(int n) {
            int i, j;
            count++;
            System.out.println("第 " + count + " 个解:");
    
            for (i = 1; i <= n; i++) // 行
            {
                for (j = 1; j <= n; j++) // 列
                {
                    if (q[i] != j)
                        System.out.print("x");
                    else
                        System.out.print("Q");
                }
                System.out.println();
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            System.out.println("请输入皇后个数:");
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            n_queens(1, n);
            System.out.println("共有 " + count + " 种摆放方式");
        }
    }
    

    解法二回溯算法

    待续
    package test;
    
    
    import java.util.Arrays;
    
    public class NhuanghouTest {
    
        static int n = 8;
        static int[] queque = new int[10];
    
        static int count=0;
    
        public static void main(String[] args) {
    
    
            trace(1);
    
    
        }
    
        public static void trace(int k){
    
            if(k>n){
                System.out.println(Arrays.toString(queque));
                count++;
                System.out.println(count);
            }else {
    
                for (int j = 0; j < n; j++) {
                    if(isSafe(k,j)){
                        queque[k]=j;
                        trace(k+1);
                        queque[k]=0;
                    }
                }
    
            }
        }
    
        private static  boolean isSafe(int row,int j) {
    
    
            for (int i = 1; i < row; i++) {
                if((queque[i]==j)||(Math.abs(row-i)==Math.abs(j-queque[i]))){
                    return false;
                }
    
            }
    
            return true;
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:n皇后问题的解法

          本文链接:https://www.haomeiwen.com/subject/llxydrtx.html