Question: I'm having trouble debugging this. It has trouble backtracking when trying to backtrack twice at once (when the previous element is in the last row)

I'm having trouble debugging this. It has trouble backtracking when trying to backtrack twice at once (when the previous element is in the last row) and only halfway backtracks it.

#include

#include

#include

using namespace std;

int main() {

int size=8;

int permrow=2, permcol=1;

permcol--; permrow--;

vector<int> inputrows;

vector<int> inputcols;

inputcols.push_back(permcol);

inputrows.push_back(permrow);

bool board[size][size];

bool rowsused[size];

//create board and set each row to unused

for (int i=0; i

rowsused[i]=false;

for (int j=0; j

board[i][j]=false;

}

}

//set the permanent pieces

for (int i=0; i

board[inputrows[i]][inputcols[i]]=true;

rowsused[inputrows[i]]=true;

}

//to track cols and rows used

int countcol=0; int countrow=0;

//makes sure to start at the first available row

for (int i=0; i

if (inputcols[i]==countcol) {

countcol++;

i=0;

}

}

bool done=false; bool permused=false; bool nosolution=false;

stack<int> savelastrow;

while (done==false) {

//check if ur on any permanently placed piece's row or column. if you are, move forward 1 spot in whichever direction necessary and restart loop

permused=false;

for (int i=0; i

if (countcol==inputcols[i]) {

countcol++;

permused=true;

break;

}

else if (countrow==inputrows[i]) {

countrow++;

permused=true;

break;

}

}

if (permused==true) {

continue;

}

//check if the spot you are about to check is already taken

if (board[countrow][countcol]==true) {

countrow++;

continue;

}

//if every row has been taken in that column, move the previous piece

if (countrow>=size) {

countcol--;

if (savelastrow.empty()) {

break;

}

rowsused[savelastrow.top()]=false;

countrow=savelastrow.top()+1;

if (countrow>=size-1) {

countrow=0;

}

board[countcol][savelastrow.top()]=false;

savelastrow.pop();

continue;

}

//check if the general row you are about to check is already taken

if (rowsused[countrow]==true) {

countrow++;

continue;

}

if (countcol<0) {

nosolution=true;

break;

}

int tempcol=countcol; int temprow=countrow;

bool hitsadiag=false;

while (tempcol>0 && temprow < size-1) {

temprow++; tempcol--;

if (board[temprow][tempcol]==true) {

hitsadiag=true;

break;

}

}

tempcol=countcol; temprow=countrow;

while (temprow>0 && tempcol < size-1) {

if (hitsadiag==true) {

break;

}

temprow--; tempcol++;

if (board[temprow][tempcol]==true) {

hitsadiag=true;

break;

}

}

tempcol=countcol; temprow=countrow;

while (temprow

if (hitsadiag==true) {

break;

}

temprow++; tempcol++;

if (board[temprow][tempcol]==true) {

hitsadiag=true;

break;

}

}

tempcol=countcol; temprow=countrow;

while (temprow>0 && tempcol > 0) {

if (hitsadiag==true) {

break;

}

temprow--; tempcol--;

if (board[temprow][tempcol]==true) {

hitsadiag=true;

break;

}

}

if (hitsadiag==true) {

countrow++;

continue;

}

board[countrow][countcol]=true;

rowsused[countrow]=true;

savelastrow.push(countrow);

countcol++;

countrow=0;

if (countcol>=size) {

break;

}

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!