-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0052-n-queens-ii.cs
48 lines (47 loc) · 1.44 KB
/
0052-n-queens-ii.cs
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
public class Solution
{
public int TotalNQueens(int n)
{
List<int> col = new List<int>(n);
List<int> diag1 = new List<int>(n);
List<int> diag2 = new List<int>(n);
int output = 0;
Backtrack(ref output, col, diag1, diag2, n, 0, n);
return output;
}
private void Backtrack(ref int output, List<int> col, List<int> diag1, List<int> diag2, int grid_size, int row, int queens_left)
{
if(queens_left == 0)
{
output++;
}
else
{
for(int c = 0; c < grid_size; c++)
{
if(CanPlace(col, diag1, diag2, row, c))
{
Place(col, diag1, diag2, row, c);
Backtrack(ref output, col, diag1, diag2, grid_size, row + 1, queens_left - 1);
Remove(col, diag1, diag2, row, c);
}
}
}
}
private bool CanPlace(List<int> col, List<int> diag1, List<int> diag2, int r, int c)
{
return !col.Contains(c) && !diag1.Contains(r + c) && !diag2.Contains(r - c);
}
private void Place(List<int> col, List<int> diag1, List<int> diag2, int r, int c)
{
col.Add(c);
diag1.Add(r + c);
diag2.Add(r - c);
}
private void Remove(List<int> col, List<int> diag1, List<int> diag2, int r, int c)
{
col.Remove(c);
diag1.Remove(r + c);
diag2.Remove(r - c);
}
}