一个八皇后解法

一个八皇后解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <math.h>
using namespace std;

#define QUEEN 8
int board[QUEEN] = {0};

void Print()
{
static int count = 0;
cout << "Solve #" << ++count << ':' << endl;
for (int line = 0; line < QUEEN; ++line)
{
for (int col = 0; col < QUEEN; ++col)
{
if (board[line] == col)
cout << "●";
else
cout << "○";
}
cout << endl;
}
cout << endl;
}

bool Check(int line)
{
for (int y = 0; y < line; ++y)
if (board[y] == board[line] || abs(board[y] - board[line]) == abs(y - line))
return false;
return true;
}

void Put(int line)
{
if (line >= QUEEN)
{
Print();
return;
}
for (int col = 0; col < QUEEN; ++col)
{
board[line] = col;
if (Check(line))
Put(line + 1);
}
}

int main()
{
Put(0);
}

Proudly powered by Hexo and Theme by Hacker
© 2021 wastecat