I have tried to utilizing the “1 generates others 7” characteristic of n queen problem. Finally, I found that it must have the knowledge of relationship between n and numbers of solutions. The program runs as the manner of generate all relative solutions of one solution that has been found by backtracking, this backtracking will terminated when one solution has been found. Using this solution to generating others solutions that is relative. The method to generating all relative solutions is easy, but difficult in terminate the loop because it is hard to know the relationship between n and numbers of all solutions. More important, it is very difficult to proving the correctness of this method. That is, it is unclear that whether or not all solutions will be found even the relationship is known (for the sake of “propagation” of solutions). It seems induction is a good idea, but I think induction can not be applied at here. In induction process, assuming this method is correct when n-1 queens (basis is n=4), it needs to proving it is correct for n queens. How? The general method is converting it to numbers and deals it with algebra. Apparently, convert the intrinsic meaning of n queen problem to numbers is very hard and it seems that n queen problem can be solved directly when the converting method is known (FA can be applied).
Following is the codes, and there is another problem in this method: how to choose the “target” solution to generating others 7 solutions. If all solutions in charge of the “target”, the speed of this algorithm will be considerable slow (remind the number of solutions relating to n). In addition, a special data structure is required to storing solutions and support mechanism for finding. I use a three dimension array (vector_instance[i] stores all solutions (column number form) beginning with i, vector_instance[i][j] stores one solution, vector_instance[i][j][k] is the element of a solution), but I think skip list is better.
Four functors to convert the directions, SE means convert South direction to East direction.
The principle is: S(x, y) + N(x, y) == (n, n); E(x, y) + W(x, y)== (n, n); S(x, y)==E(y, n-x).
template<class T>
struct functor_SE : public unary_function<T,void>
{
functor_SE(int d):array_dim(d-1){}
void operator()(T& p)
{
swap(p.first, p.second);
p.second = array_dim-p.second;
}
int array_dim;
};
template<class T>
struct functor_EW : public unary_function<T,void>
{
functor_EW(int d):array_dim(d-1){}
void operator()(T& p)
{
p.first = array_dim-p.first;
p.second = array_dim-p.second;
}
int array_dim;
};
template<class T>
struct functor_WN : public unary_function<T,void>
{
functor_WN(int d):array_dim(d-1){}
void operator()(T& p)
{
swap(p.first, p.second);
p.first = array_dim-p.first;
}
int array_dim;
};
These four functors have been used in for_each to modify a vector that contains pairs of chesses positions. The others codes have been omitted.
Till now, n queen problem has been explored in detail. In this attempt, frustration comes from the difficulty of converting meaning of n queen problem to algebraic representation. This converting is an impossible mission at present because even the flying number problem is considerable difficult. Numbers be abstract tool is very useful in situation which cope with complex of this world. Like the algebraic representation of chesses position, it is found a special permutation according with n queen problem. To go a more step in which converting whole meaning of n queen to algebraic representation, failure is inevitable. Number theory is limited due to its method that uses number to research number. If the inherent nature is existing, number theory will never attain it. Of course, it is my thought.