#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
/**
* Place it there, applicable?
*/
bool place(int* paraSolution, int paraT){
int j;
for (j = 1; j < paraT; j ++){
if ((abs(paraT - j) == abs(paraSolution[j] - paraSolution[paraT])) || (paraSolution[j] == paraSolution[paraT]))
return false;
}//Of for
return true;
}//Of place
/**
* Backtracking.
*/
void backtracking(int* paraSolution, int paraN, int paraT){
int i;
if (paraT > paraN){
for (i = 1; i <= paraN; i ++)
printf("%d ", paraSolution[i]);
printf("\r\n");
}else{
for (i = 1; i <= paraN; i ++){
paraSolution[paraT] = i;
if (place(paraSolution, paraT))
backtracking(paraSolution, paraN, paraT + 1);
}//Of for i
}//Of if
}//Of backtracking
/**
* Title: n queens.<br>
*/
void nQueen(int paraN){
int i;
int* solution = (int*)malloc((paraN + 1) * sizeof(int));
for (i = 0; i <= paraN; i ++)
solution[i] = 0;
backtracking(solution, paraN, 1);
}//Of nQueen
/**
* The entrance.
*/
int main(){
nQueen(5);
return 1;
}//Of main
05-03
7713