码迷,mamicode.com
首页 > 其他好文 > 详细

8皇后问题的各种解法

时间:2016-07-10 13:53:44      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

总体上分为回溯和全排列

全排列(递归)

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 template <size_t N> struct ArraySizeHelper { char _[N]; };
 7 template <typename T, size_t N> ArraySizeHelper<N> makeArraySizeHelper(T(&)[N]);
 8 #define ARRAY_SIZE(a) sizeof(makeArraySizeHelper(a))
 9 
10 bool valid_permutation(const int *queen, int len)
11 {
12     bool valid = true;
13 
14     for (int i = 0; i < len; ++i)
15     {
16         for (int j = i + 1; j < len; ++j)
17         {
18             if (queen[j] - queen[i] == j - i || queen[j] - queen[i] == i - j)
19             {
20                 valid = false;
21             }
22         }
23     }
24 
25     return valid;
26 }
27 
28 void permutation(int *queen, int len, int idx, int &count)
29 {
30     if (idx == len)
31     {
32         if (valid_permutation(queen, len)) ++count;
33     }
34     else
35     {
36         for (int i = idx; i < len; ++i)
37         {
38             swap(queen[i], queen[idx]);
39             permutation(queen, len, idx + 1, count);
40             swap(queen[i], queen[idx]);
41         }
42     }
43 }
44 
45 // Solved by permutation recursion.
46 int eightqueen_permutation_recur()
47 {
48     int queen[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
49     int count = 0;
50 
51     permutation(queen, (int)ARRAY_SIZE(queen), 0, count);
52 
53     return count;
54 }
55 
56 int main()
57 {
58     cout << eightqueen_permutation_recur() << endl;
59 
60     system("pause");
61     return 0;
62 }

 

 回溯(递归)

#include <iostream>
#include <algorithm>

using namespace std;

template <size_t N> struct ArraySizeHelper { char _[N]; };
template <typename T, size_t N> ArraySizeHelper<N> makeArraySizeHelper(T(&)[N]);
#define ARRAY_SIZE(a) sizeof(makeArraySizeHelper(a))



bool valid_backtracking(const int *queen, int len)
{
    for (int i = 0; i < len; ++i)
    {
        const int diff = abs(queen[i] - queen[len]);
        if (diff == 0 || diff == len - i) return false;
    }

    return true;
}

void placequeen(int *queen, int len, int idx, int &count)
{
    if (idx == len)
    {
        ++count;
    }
    else
    {
        for (int i = 0; i < len; ++i)
        {
            queen[idx] = i;

            if (valid_backtracking(queen, idx))
            {
                placequeen(queen, len, idx + 1, count);
            }
        }
    }
}

// Solved by backtracking(DFS) recursion.
int eightqueen_backtracking_recur()
{
    int queen[8];
    int count = 0;

    placequeen(queen, (int)ARRAY_SIZE(queen), 0, count);

    return count;
}


int main()
{
    cout << eightqueen_backtracking_recur() << endl;

    system("pause");
    return 0;
}

 参考:http://www.cnblogs.com/codingmylife/archive/2012/10/04/2711839.html

8皇后问题的各种解法

标签:

原文地址:http://www.cnblogs.com/raichen/p/5655506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!