The backtracking algorithm of n queen problem can be improved by space and time trade-off further. The consideration of positions occupancy is attained by duplicate_array and two integrals. Note that these utilities can’t tell all illegal moves because it is performing its job on the basis of chess_board definition. That is, power of utilities contained by chess_board are not enough to telling all illegal moves. It needs a data structure to record positions occupied by chesses. Naturally, it needs three arrays to recording row, column, diagonal1 (upper left to lower right) and diagonal2 (upper right to lower left). Column is already represented by duplicate_array. So it needs two other array to represent diagonal1,2. Consequently, the job is how to express the meaning of one diagonal is occupied. That is, for a move: (cur_i, j), how to assign value to the corresponding element in diagonal1,2. Assuming the one of occupied diagonal1 is diagonal1[x] and diagonal2 is diagonal2[y], then assign diagonal1[x] and diagonal2[y] to 1 to denoting these diagonals are occupied. An n queen problem has 2n-1 diagonal1s and 2n-1 diagonal2s. The rules are:
Diagonal1[j-cur_i]=1 if j >= cur_i
Diagonal1[j-cur_i+2*n-1]=1 if j < cur_i
Diagonal2[j+cur_i]=1
The codes:
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <ctime>
using namespace std;
template<class T>
struct output_functor : public unary_function<T,void>
{
output_functor():n(0){}
void operator()(const T& p)
{
cout<<"("<<n<<","<<p<<")"<<" ";
n++;
}
int n;
};
class chess_board
{
vector<int> array;
vector<bool> duplicate_array;
vector<bool> diagonal1, diagonal2;
public:
int n;
int cur_i;
int number_solution;
chess_board(int dim):n(dim),cur_i(0),number_solution(0)
{
array.resize(n,-30000);
duplicate_array.resize(n,0);
diagonal1.resize(2*n-1,0);
diagonal2.resize(2*n-1,0);
}
void move(int j)
{
array[cur_i]=j;
if(j>=cur_i)
{
diagonal1[j-cur_i]=1;
}
else
{
diagonal1[j-cur_i+2*n-1]=1;
}
diagonal2[j+cur_i]=1;
duplicate_array[j]=1;
}
void un_move(int j)
{
array[cur_i]=-30000;
if(j>=cur_i)
{
diagonal1[j-cur_i]=0;
}
else
{
diagonal1[j-cur_i+2*n-1]=0;
}
diagonal2[j+cur_i]=0;
duplicate_array[j]=0;
}
bool legal_pos(int j)
{
if(duplicate_array[j] == 0 && diagonal2[j+cur_i] == 0)
{
if(j<cur_i)
{
if(diagonal1[j-cur_i+2*n-1]==0)
return true;
}
else
{
if(diagonal1[j-cur_i]==0)
return true;
}
return false; //tricky. if lack this, then function has nothing to return(it will return true in this situation)
}
else
return false;
}
void write()
{
number_solution++;
for_each(array.begin(), array.end(), output_functor<int>());
cout<<endl;
}
};
void BackTracking(chess_board_delegation& c)
{
if(c.get_cur_i() == c.get_n())
{
c.write();
c.set_cur_i(c.get_cur_i()-1);
return;
}
else
{
for(int j=0; j<c.get_n(); j++)
{
if(c.legal_pos(j) == false)
continue;
c.move(j);
c.set_cur_i(c.get_cur_i()+1);
BackTracking(c);
c.un_move(j);
}
c.set_cur_i(c.get_cur_i()-1);
}
}
//////////////////////// omitted chess_board_delegation and main ////////////////////////////
For the convenient of test, it should use a template argument to indicate with or without output.
template<bool with_output>
class chess_board
{
......
void write()
{
if(with_output == true)
{
number_solution++;
for_each(array.begin(), array.end(), output_functor<int>());
cout<<endl;
}
else
{
number_solution++;
}
}
......
};
template<bool T>
class chess_board_delegation
{
chess_board<T>* cbp;
public:
chess_board_delegation(chess_board<T>* p):cbp(p){}
......
};
template<bool T>
void BackTracking(chess_board_delegation<T>& c)
{
......
}
int main()
{
......
chess_board<false> a(n);
chess_board_delegation<false> c(&a);
......
}
Using specialization can avoid the equality comparison in write function in order to improve the performance a bit. The codes of definition of chess_board is: template<bool with_output> class chess_board{…with two last sentences of write function…}; template<>class chess_board<false>{…without two last sentences of write function…}; But this method needs double codes so I have not write it at here.
There are a lot of works has done, the practice data shows that: for n=13, the first version needs: 10.956 seconds. The best (last) version needs 1.963 seconds. (Number of solutions are 73712) of course, the larger n is, the improvement has. It still has room to improve performance of this algorithm because symmetry. Utilize this characteristic of n queen problem can lead to double performance improvement. I leave this in next article.