'Eight Queens C++ utilizing a Stack Backtracking

So I'm doing this for homework, and I can't figure out where my bug is. Any help is greatly appreciate.

My understanding is this.

  1. Initialize a stack for tracking which row & column has a queen in it.
  2. Place a queen on the first square, push its location onto the stack. Push (0,0); Then set a variable that the row has been filled. filled+;

3.Then Loop

check if the current row or column have a conflict with another queen.

a. no conflict. push to stack. increase the filled row variable. filled++; move up a row.

b. there is a conflict. Move right. col++;

c. cant move right anymore. pop the stack and set to row and col. subtract the filled. then move over. col++; and try again.


int main(){


bool board[8][8];

for(int i = 0; i < 8; i++){
    for(int j = 0; j < 8; j++){
        board[i][j] = false;}}

int row = 0, col = 0, filled = 0;

StackLi<int> rowStack;
StackLi<int> colStack;

rowStack.push(row);
colStack.push(col);

board[row][col] = true;
//cout << "push: " << "(" << row << "," << col << ")" << endl;  
row++;


while(filled < 7)
{
    if(!isSafe(board,row,col) )
        {
        filled++;
        rowStack.push(row);
        colStack.push(col);
        board[row][col] = true;
    //cout << "push: " << "(" << row << "," << col << ")" << endl;  
        if(filled > 8)
            {
            print(board);
            return 0;
            }
        row++;
    }
    else{
col++;
    //cout << "move: " << "(" << row << "," << col << ")" << endl;  
    }

    if(col > 7)
{
       row = rowStack.topAndPop();
       col = colStack.topAndPop();
   board[row][col] = false;
       cout << "pop: " << "(" << row << "," << col << ")" << endl;  
   filled--;
}

}

return 0;

}

bool isSafe(bool board[8][8], int row, int col)
{


for(int i = 0; i < 8; i++)
{
    if(board[row][i] || board[i][col]) return true;
}

for(int i = 0; (row - i)>=0 && (col-i) >= 0; i++)
{
    if(board[row-i][col-i]) return true;
}

for(int i = 0; (row - i)<=8 && (col-i) >= 0; i++)
{
    if(board[row+i][col+i]) return true;
}

return false;

}


Solution 1:[1]

#include <iostream>
using namespace std;
class Node {
public:
    int data;
    Node* next;
    Node()
    {
        data = 0;
        next = NULL;
    }
};
class Stack {
    Node* top;
public:
    Stack()
    {
        top = NULL;
    }
    bool is_empty()
    {
        if (top == NULL)
            return true;
        else
            return false;
    }
    void push(int item)
    {
        Node* new_node = new Node();
        new_node->data = item;
        if (is_empty())
        {
            new_node->next = NULL;
            top = new_node;
        }
        else {
            new_node->next = top;
            top = new_node;
        }
    }
    int pop() {
        int value;
        Node* delet_ptr = top;
        value = top->data;
        top = top->next;
        delete delet_ptr;
        return value;
    }
    };

bool isSafe(bool board[8][8], int row, int col)
{
    for (int i = 0; i < 8; i++)
    {
        if (board[row][i] || board[i][col]) {return true;}
    }

    for (int r = row, c = col; r < 8 && c >=0; r++,c--)
    {
        if (board[r][c]) {return true;}
    }

    for (int r = row, c = col; r < 8 && c < 8; r++,c++)
    {
        if (board[r][c]) {return true;}
    }

    for (int r = row, c = col; r >= 0 && c >= 0; r--,c--)
    {
        if (board[r][c]) {return true;}
    }

    for (int r = row, c = col; r >= 0 && c < 8; r--,c++)
    {
        if (board[r][c]) {return true;}
    }

    return false;
}

    int main() {
        bool board[8][8];

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                board[i][j] = false;
            }
        }

        int row = 0, col = 0, filled = 0;
        Stack rowStack, colStack;

        rowStack.push(row);
        colStack.push(col);

        board[row][col] = true;
        cout << "push: " << "(" << row << " , " << col << ")\n" << endl;  
        row++;


        while (filled < 7)
        {
            if (!isSafe(board, row, col))
            {
                filled++;
                rowStack.push(row);
                colStack.push(col);
                board[row][col] = true;
                cout << "push: " << "(" << row << " , " << col << ")" << endl;  
                row++;
                col=0;
            }else {
                col++;
                cout << "move: " << "(" << row << " , " << col << ")" << endl;
                if (col > 7) {
                    row = rowStack.pop();
                    col = colStack.pop();
                    board[row][col] = false;
                    cout << "pop: " << "(" << row << " , " << col << ")" << endl;
                    col++;
                    filled--;
                }
            }
        }
        system("pause");
        return 0;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdo Shaisha