using System.Collections.Generic;
public static void Main()
NQueen nQueen = new NQueen();
List<IList<string>> ans = new List<IList<string>>();
rowPlacements = Enumerable.Repeat(-1, length).ToList();
private void DFS(int row)
for (int col = 0; col < length; col++)
if (IsSafeToPlace(row, col))
rowPlacements[row] = col;
private bool IsSafeToPlace(int row, int col)
for (int r = 0; r < length; r++)
if (rowPlacements[r] != -1 &&
(rowPlacements[r] == col ||
r + rowPlacements[r] == row + col ||
r - rowPlacements[r] == row - col))
List<string> list = new List<string>();
for (int i = 0; i < length; i++)
List<string> rowList = Enumerable.Repeat(".", length).ToList();
rowList[rowPlacements[i]] = "Q";
list.Add(string.Join("", rowList));
private void PrintAnswer()
for (int i = 0; i < ans.Count(); i++)
Console.WriteLine(string.Format("{1}// Solution {0}", i + 1, Environment.NewLine));
Console.WriteLine(string.Join(Environment.NewLine, ans[i]));