0

this is what I have so far. I did most of the work just that my output is screwed

#include <iostream>
#include <fstream>
#include <stack>
#include <cmath>
#include <cassert>

using namespace std;


int number();

bool Valid( stack<int> s, int row){// This will find out if there is a valid solution for the K by K int given.
     bool answer(false);
     stack<int> hold;
     int column(0);
     int ORow(0);
     int OCol(0);
     if(!s.empty()){
       column = s.top();
       hold.push(s.top());
       s.pop();
     }
     if(!s.empty()){
       OCol = s.top();
       ORow = row - 1;
     }
     while(!s.empty() && !answer){
        if((column == OCol)||(abs(column - OCol) == abs(row - ORow))){
          answer = true;
        }
        else{
          answer = false;
          hold.push(s.top());
          s.pop();
        }
        if(!s.empty()){
          OCol = s.top();
          ORow = ORow - 1;
        }
     }
     return answer;
}

void Advance(stack<int> & s, int & rows, const int k){// This sees if there is any complication between the queens
     int number(0);
     
     while(!s.empty() && s.top() == k){
          s.pop();
          rows = rows - 1;
        }
     if(!s.empty()){
          number = s.top() + 1;
          s.pop();
          s.push(number);
}
     }
int main(int integer){//Initialization of all functions and output of queens problem using an array..
    stack<int> s;
    int rows(0);
    int k(0);
    bool goodMove(false);
    bool NA(false);
    int array[integer];
    int index(integer-1);
    int row(1);
    
    cout << "Input Size of Chessboard." << endl;
    cin >> k;
    
    s.push(1);
    rows = rows + 1;
    
    while(!goodMove && !s.empty()){
       NA = Valid( s, rows);
       if(NA)
         Advance( s, rows, k);
       else if(!NA && (s.size() == k))
          goodMove = true;
       else{ 
         s.push(1);
         rows = rows + 1;
       }
    }
   if(goodMove){
       while(!s.empty()){
          array[index] = s.top();
          s.pop();
          index = index - 1;
       }
       while(row <= integer){
         index = index + 1;
         cout << "Row "<< row << " Column "<< array[index] << endl;
         row = row + 1;
       }
       }
     else{
       cout << "No solution"  <<endl;
     }
     

    return 0;    
}
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Nick Evan
0

"my output is screwed" is not a great description of your problem.
Please explain what output you are expecting, and what output you get.
Also explain what the program is suppose to do.

Niek

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.