IN C++ N-Queens. Implement the solution for the N-Queens problem using stacks. E
ID: 3749460 • Letter: I
Question
IN C++ N-Queens. Implement the solution for the N-Queens problem using stacks. Either define a fixed N or let the user define N (this should not affect the algorithm or its implementation details). You do not need to implement your own stack. CpR file(s) THIS IS THE PSUDOCODE Initialize stack push first queen position onto the stack, and filled-0 Repeat: if no conflicts with queens filledfilled+1; if filled N done else t move to next row and place a queen in first column else if a conflict and there is room to shift move current queen to the right, adjusting the stack record (new position) else if there is a conflict and there is no room to shift { Backtrack: keep popping the stack and filled-filled-1 until you reach a row where the queen can be shifted; shift that queenExplanation / Answer
//can define a class board having attributes rows and coloumns. we will be using such a type in our stack
//writing in a function for usability, the fuction returns a boolean value
bool safe_queen(stack<board> ori_stack, board ori_queen) {
stack<board> test;
board test_queen;
while (!test.empty())
{
test_queen = testS.top();
if (ori_queen.col == test_queen.col)
{
return false;
}
test.pop();
}
test = ori_stack;
while (!test.empty())
{
test_queen = test.top();
if ((test.col - test_queen.row) == (ori_queen.col - ori_queen.row))
{
return false;
}
test.pop();
}
return true;
}