```
bool valid_q( int row, int col, int* board, int size );
void do_solve( int row, int n, int* answer, vector<vector<string> >& result_vector );
class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
const int INITIAL = -999;
vector<vector<string> > re_so;
//When I use memset here, the answer went wrong,
//I can only assign the value one by one
//there is no compile error nor the runtime error
//it cannot pass the test case when n = 4
int* a = new int[n]; //the answer set
//memset( a, INITIAL, n );
for( int i = 0; i < n; i++ )
a[i] = INITIAL;
do_solve( 0, n, a, re_so );
return re_so;
}
};
void do_solve( int row, int n, int* board, vector<vector<string> >& re_so)
{
const int INITIAL = -999;
if( row == n )
{
//get one result
vector<string> temp_so;
//we first record the solution, then find the next
for( int index = 0; index < n; index++ )
{
string temp( n, '.' );
temp[board[index]] = 'Q';
temp_so.push_back(temp);
}
re_so.push_back( temp_so );
return;
}
for( int k = 0; k < n; k++ ) //test for each col
{
if( valid_q( row, k, board, n) ) //we can put the queen here
{
board[row] = k;
do_solve( row+1, n, board, re_so ); //we find place for next line
board[row] = INITIAL; //when we back to this line, reset it
}
}
}
//test if row i and col j is able to set a queen
//a simple way to test, though exists many other ways
bool valid_q( int row, int col, int* board, int size )
{
for( int i = 0; i < size; i++ )
{
//we write seprately, sometimes do one less judge
if( *(board+i) == col ) //means this col is used already
return false;
else if( abs(row - i) == abs(col - *(board+i)) ) //in the same diagonal
return false;
}
return true;
}
```