using System.Collections; struct IntPair { public readonly int First, Second; public IntPair(int fst, int snd) { this.First = fst; this.Second = snd; } } class Conflict : System.Exception {} public class Queens { private static bool safe(int x, int y, IEnumerable positions) { foreach(IntPair p in positions) { int x1 = p.First, y1 = p.Second; if (x == x1 || y == y1 || y - x == y1 - x1 || y + x == y1 + x1) { return false; } } return true; } private static Stack queens(int n, int x, int y, Stack positions) { if (x > n) { return positions; } else if (y > n) { throw new Conflict(); } else { if (safe(x, y, positions)) { positions.Push(new IntPair(x, y)); try { return queens(n, x+1, 1, positions); } catch (Conflict c) { positions.Pop(); } } return queens(n, x, y+1, positions); } } public static void Main (string [] args) { int n = System.Int32.Parse(args[0]); Stack s = new Stack(); s = queens(n, 1, 1, s); foreach(IntPair p in s) { System.Console.WriteLine ("{0}, {1}", p.First, p.Second); } } }