int main() { int n; printf("Enter the number of queens : "); scanf("%d", &n); nQueen(1, n); if(flag == 0) printf("Queens cannot be placed. Please! Try again with other number.\n"); return 0; }
// nQueen(1); void nQueen(int queen) { for(int row = 1; row <= 8; row++) { if (isPlacable(queen, row)) { x[queen] = row; // found a solution if (queen == 8) { for(int i = 1; i <= 8; i++) { // print } return; } nQueen(queen + 1); } } }
void nQueen(int k, int n) { int i, j; if(k<=n) { for(i=1; i<=n; i++) { if(place(k, i)) { x[k] = i; nQueen(k+1, n); } } } else { flag = 1; printf("The solutions are :\n"); for(j=1; j<=n; j++) printf("%d ", x[j]); printf("\n"); } }