Answer:
Board[n] = {}
bool isColliding(int row, int col)
{
for ( i = 1; i < row; ++i)
{
/* diagonal collision */
if ( abs(row -i) == abs(col - Board[i] ) )
return false;
/* columnar collision */
if ( Board[i] == col )
return false;
}
return true;
}
void placeQueen(int row, int col)
{
Board[row] = col;
}
void Iterative_NQueens()
{
for (row=1; row <= n; ++row)
{
for(col=1; col <= n; ++col )
{
if ( isColliding(row,col) )
continue;
else
{
placeQueen(row,col);
if ( row == n )
{
/* print the solution */
}
break; // continue with next row
}
}
}
}
void Recursive_NQueens(int _row)
{
for (row = _row; row <= n; ++row)
{
for(col=1; col <= n; ++col )
{
if ( isColliding(row,col) )
continue;
else
{
placeQueen(row,col);
if ( row == n )
{
/* print the solution */
return;
}
else
/* next row */
Recursive_NQueens(_row + 1);
}
}
}
}
No comments:
Post a Comment