zoukankan      html  css  js  c++  java
  • n后问题

    在n*n的棋盘上放置彼此不受攻击的n个皇后,按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。n后问题等价于在n*n格的棋盘上放置n个皇后,任何2个皇后不放在同一行或同一列或同一斜线上。共有多少中放法?

    1.回溯法

    #include <stdio.h>
    #include <stdlib.h>
    
    int *x;
    int n;
    int sum;
    
    int place(int k)
    {
        for (int i = 1; i < k; i++) {
            if (x[i] == x[k] || abs(x[i] - x[k]) == abs(i - k)) {
                return 0;
            }
        }
        return 1;
    }
    
    void backtrack(int t)
    {
        if (t > n) sum++;
        else
        {
            for (int i = 1; i <= n; i++) {
                x[t] = i;
                if (place(t)) {
                    backtrack(t+1);
                }
            }
            
        }
    }
    
    int main()
    {
        printf("请输入n的值:
    ");
        scanf("%d", &n);
        
        int a[n+1];
        x = a;
        
        sum = 0;
        backtrack(1);
        
        printf("当n = %d时,共有%d种不同的放置方法.", n, sum);
        
        return 0;
    }
    


    2. 暴力求解

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, const char * argv[])
    {
        int num, n;
        int count = 0;
        int *x;
        
        printf("请输入n的值:
    ");
        scanf("%d", &n);
        
        int a[n+1];
        x = a;
        
        for (num = 0; num < pow(4, 4); num++) {
            int j = num;
            for (int i = 0; i < n; i++) {
                x[i] = j % n;
                j = j / n;
            }
            
            int k = 1;
            for (int i = 0; i < n; i++) {
                for (j = i + 1; j < n; j++) {
                    if (a[i] == a[j] || abs(i - j) == abs(a[i] - a[j])) {
                        k = 0;
                    }
                }
            }
            
            if (k) {
                count++;
            }
        }
        
        printf("当n = %d时,共有%d中不同的放置方法.
    ", n, count);
    
        return 0;
    }
    





    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    eclipse下jsp文件报错解决方法
    使用vscode搭建本地的websocket
    tomcat的首次登录配置
    tomcat配置报错解决方法 The jre_home environment variable is not defined correctly
    cento升级openssl依旧显示老版本
    Centos6安装mysql5.7最新版
    Neutron服务组件
    网络OSI 7层模型
    Kubernetes的核心技术概念和API对象
    Xen 虚拟化技术
  • 原文地址:https://www.cnblogs.com/liuqblog/p/4948158.html
Copyright © 2011-2022 走看看