ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

全排列问题和八皇后问题

2021-05-29 23:55:49  阅读:121  来源: 互联网

标签:11 index 排列 false int 问题 maxn 皇后 include


全排列问题的解法如下:

#include <stdio.h>
const int maxn = 11;

int n, p[maxn], hashTable[maxn] = { false };

void generateP(int index){
    if (index == n + 1){
        for (int i = 1; i < n; i++){
            printf("%d", p[i]);                //输出当前排列
        }

        printf("\n");
        return;
    }

    for (int x = 1; x <= n; x++){
        if (hashTable[x] == false){
            p[index] = x;
            hashTable[x] = true;
            generateP(index + 1);            //处理排列的第index + 1号位
            hashTable[x] = false;            //已处理完p[index]为x的子问题,还原状态
        }
    }
}

int main()
{
    n = 3;
    generateP(1);
}

解析如下:(取自《算法笔记》)

运用这个全排列解法可以解决很多需要全排列来解决的问题,如8皇后问题:

8皇后问题的解法如下:

法一:

#include <stdio.h>
#include <math.h>
const int maxn = 11;

int n, p[maxn], hashTable[maxn] = { false };
int count = 0;

void generateP(int index){
    if (index == n + 1){        
        int flag = true;
        for (int i = 1; i <= n; i++){            //两两比较皇后,如果发现在同一斜线上,flag = false;
            for (int j = i + 1; j <= n; j++){
                if (abs(i - j) == abs(p[i] - p[j])){
                    flag = false;            //不合法
                }
            }
        }
        if (flag){
            count++;
        }
            return;
    }

    for (int x = 1; x <= n; x++){
        if (hashTable[x] == false){
            p[index] = x;
            hashTable[x] = true;
            generateP(index + 1);            //处理排列的第index + 1号位
            hashTable[x] = false;            //已处理完p[index]为x的子问题,还原状态
        }
    }
}

int main()
{
    n = 8;
    generateP(1);
    printf("%d", count);

}

优化后的算法:法二:

 1 #include <stdio.h>
 2 #include <math.h>
 3 const int maxn = 11;
 4 
 5 int n, p[maxn], hashTable[maxn] = { false };
 6 int count = 0;
 7 
 8 void generateP(int index){
 9     if (index == n + 1){
10         count++;            //能到这里说明已经是满足条件了
11         return;
12     }
13 
14     for (int x = 1; x <= n; x++){
15         if (hashTable[x] == false){
16             bool flag = true;
17             //判断当前index的皇后是否会和之前的皇后在同一斜线上
18             for (int i = 1; i < index; i++){
19                 if (abs(i - index) == abs(p[i] - x)){
20                     flag = false;
21                     break;
22                 }
23             }
24 
25             if (flag){
26 
27                 p[index] = x;
28                 hashTable[x] = true;
29                 generateP(index + 1);            //处理排列的第index + 1号位
30                 hashTable[x] = false;            //已处理完p[index]为x的子问题,还原状态
31             }
32         }
33     }
34 }
35 
36 int main()
37 {
38     n = 8;
39     generateP(1);
40     printf("%d", count);
41 
42     return 0;
43 }

 

标签:11,index,排列,false,int,问题,maxn,皇后,include
来源: https://blog.51cto.com/u_14201949/2832187

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有