C# Source Code
// eight-queens
// Author: Hannes du Plooy
// Date: 20 Sep 2016
// Objective: To solve the following
// Search for a chess board solution with 8 queens and no one
// endangering any other.
using System;
using System.IO;
namespace EightQueensNS {
public class EightQueensClass {
static int[] board = new int[8];
static bool EightQueens(int line) {
if (line > 7) {
return true;
}
for(int column=0;column<8;column++) {
bool endangered = false;
for(int line2=0;line2<line;line2++) {
if (board[line2] == column || Math.Abs(line-line2) == Math.Abs(column-board[line2])) {
endangered = true;
break;
}
}
if (!endangered) {
board[line] = column;
if (EightQueens(line+1)) {
return true;
}
}
}
return false;
}
static void Main(string[] args) {
if (EightQueens(0)) {
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
if (board[i] == j) {
Console.Write("Q");
} else {
Console.Write(".");
}
}
Console.WriteLine();
}
} else {
Console.WriteLine("No Solution");
}
}
}
}