-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueensPermutations.cs
More file actions
36 lines (33 loc) · 962 Bytes
/
NQueensPermutations.cs
File metadata and controls
36 lines (33 loc) · 962 Bytes
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
using System;
namespace DA.Algorithms.Problems
{
public static class NQueensPermutations
{
public static void NQueens (int[] queens, int position, int chessBoardLength)
{
if (position == chessBoardLength)
{
return;
}
for (int i = 0; i < chessBoardLength; i++)
{
queens[position] = i;
if (IsFeasible (queens, position))
{
NQueens (queens, position + 1, chessBoardLength);
}
}
}
private static bool IsFeasible (int[] queens, int position)
{
for (int i = 0; i < position; i++)
{
if (queens[position] == queens[i] || Math.Abs (queens[position]) == Math.Abs (i - position))
{
return false;
}
}
return true;
}
}
}