예제 #1
0
        public static IList <IList <string> > SolveNQueens(int n)
        {
            var output = new List <IList <string> >();
            var q      = new QueenBoard(n);

            SolveNQueenHelper(q, 0, output);
            return(output);
        }
예제 #2
0
        public static void SolveNQueenHelper(QueenBoard board, int row, IList <IList <string> > solutions)
        {
            if (row == board.Size)
            {
                solutions.Add(board.ToStringList());
                return;
            }

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