-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (50 loc) · 1.13 KB
/
Program.cs
File metadata and controls
55 lines (50 loc) · 1.13 KB
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
53
54
55
using System;
namespace n_queens_dotnet
{
class Program
{
const int queenCount = 13;
static int[] queenList;
static void Main(string[] args)
{
testEightQueens();
}
static void testEightQueens()
{
queenList = new int[queenCount];
putQueen(0);
}
static bool checkConflict(int nextY)
{
for (int positionY = 0; positionY < nextY; positionY++)
{
if (Math.Abs(queenList[positionY] - queenList[nextY]) == Math.Abs(positionY - nextY) || queenList[positionY] == queenList[nextY])
{
return true;
}
}
return false;
}
static int count = 0;
static void putQueen(int nextY)
{
for (queenList[nextY] = 0; queenList[nextY] < queenCount; queenList[nextY]++)
{
if (checkConflict(nextY) == false)
{
nextY++;
if (nextY < queenCount)
{
putQueen(nextY);
}
else
{
count++;
Console.WriteLine(count.ToString() + ": " + string.Join(", ", queenList));
}
nextY--;
}
}
}
}
}