예제 #1
0
        public static int SolveNQueenHelper(QueenBoard board, int row, int numSolutions)
        {
            if (row == board.Size)
            {
                return(numSolutions + 1);
            }

            for (var col = 0; col < board.Size; col++)
            {
                var successfulPlacement = board.TryPlaceQueen(row, col);
                if (successfulPlacement)
                {
                    numSolutions = SolveNQueenHelper(board, row + 1, numSolutions);
                    board.RemoveQueen(row, col);
                }
            }

            return(numSolutions);
        }
예제 #2
0
        public static int TotalNQueens(int n)
        {
            var q = new QueenBoard(n);

            return(SolveNQueenHelper(q, 0, 0));
        }