18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
BacktrackingNQueensSolver.cs 3.07 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2023-12-31 09:48 . Switch to file-scoped namespaces (#431)
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;
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助