forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BacktrackingNQueensSolver.cs
108 lines (95 loc) · 3.43 KB
/
BacktrackingNQueensSolver.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
namespace Algorithms.Problems.NQueens
{
public class BacktrackingNQueensSolver
{
/// <summary>
/// Solves N-Queen Problem given a n dimension chessboard and using backtracking with recursion algorithm.
/// If we find a dead-end within or current solution we go back and try another position for queen.
/// </summary>
/// <param name="n">Number of rows.</param>
/// <returns>All solutions.</returns>
public IEnumerable<bool[,]> BacktrackSolve(int n)
{
if (n < 0)
{
throw new ArgumentException(nameof(n));
}
return BacktrackSolve(new bool[n, n], 0);
}
private static IEnumerable<bool[,]> BacktrackSolve(bool[,] board, int col)
{
var solutions = col < board.GetLength(0) - 1
? HandleIntermediateColumn(board, col)
: HandleLastColumn(board);
return solutions;
}
private static IEnumerable<bool[,]> HandleIntermediateColumn(bool[,] board, int col)
{
// To start placing queens on possible spaces within the board.
for (var i = 0; i < board.GetLength(0); i++)
{
if (CanPlace(board, i, col))
{
board[i, col] = true;
foreach (var solution in BacktrackSolve(board, col + 1))
{
yield return solution;
}
board[i, col] = false;
}
}
}
private static IEnumerable<bool[,]> HandleLastColumn(bool[,] board)
{
var n = board.GetLength(0);
for (var i = 0; i < n; i++)
{
if (CanPlace(board, i, n - 1))
{
board[i, n - 1] = true;
yield return (bool[,])board.Clone();
board[i, n - 1] = false;
}
}
}
/// <summary>
/// Checks whether current queen can be placed in current position,
/// outside attacking range of another queen.
/// </summary>
/// <param name="board">Source board.</param>
/// <param name="row">Row coordinate.</param>
/// <param name="col">Col coordinate.</param>
/// <returns>true if queen can be placed in given chessboard coordinates; false otherwise.</returns>
private static bool CanPlace(bool[,] board, int row, int col)
{
// To check whether there are any queens on current row.
for (var i = 0; i < col; i++)
{
if (board[row, i])
{
return false;
}
}
// To check diagonal attack top-left range.
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--)
{
if (board[i, j])
{
return false;
}
}
// To check diagonal attack bottom-left range.
for (int i = row + 1, j = col - 1; j >= 0 && i < board.GetLength(0); i++, j--)
{
if (board[i, j])
{
return false;
}
}
// Return true if it can use position.
return true;
}
}
}