C/C++ Source Code

/* 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. */

#include 
#include 

int board[8];

int EightQueens(int line) {
    if (line >= 8)
        return 1;
    for(int column=0;column<8;column++) {
        int endangered = 0;
        for(int line2=0;line2<line;line2++) {
            if (board[line2] == column || (abs(line-line2) == abs(column-board[line2]))) {
                endangered = 1;
                break;
            }
        }
        if (!endangered) {
		    board[line] = column;
		    if (EightQueens(line+1)) {
		        return 1;
		    }
		}
    }
    return 0;
}

int main(int argc, char **argv) {
    if (EightQueens(0)) {
        for(int i=0;i<8;i++) {
            for(int j=0;j<8;j++) {
                printf("%c",board[i]==j?'Q':'.');
            }
            printf("\n");
        }
    } else {
        printf("No solution\n");
    }
}