Trying to solve Nqueens problem would appreciate any help with getting this to r
ID: 3631738 • Letter: T
Question
Trying to solve Nqueens problem would appreciate any help with getting this to run. Thanks!#include <iostream>
#include <stdlib.h>
#include <stack>
#include <cassert>
using namespace std;
//Prints n Queens problem solution
void print (int row, stack<int> column)
{
for (int i = row-1; i >= 0; i--) {
cout<< " column " << column.top() << " , " << "row" << i << endl;
column.pop();
}
return;
}
void backtrack(stack<int> column)
{
assert(column.top() >= 1); //Checking member function
column.pop();
}
bool ok(stack<int> column, int row)
{
for(int i = 0; i < row; i++)
if(column.top()== i || (row-i) == abs(column.top() - i))
return false;
return true;
}
int main()
{
stack<int> column;
int n; //user input
int row = 0;
row ++;
column.push(1);
bool frombacktrack = 0;
bool good = 0;
cout << "n-Queens Solution" << endl
<< "---------------------------" << endl
<< "---------------------------" << endl;
cout << "Please enter an integer: ";
cin >> n;
while(true)
{ //first loop
while (row < n)
{ // second loop
if(frombacktrack)
{
row++;
frombacktrack=0;
break;
}
while(row < n)
{
if(ok(column, row))
{
good = 1;
}
row++;
}
if(good)
{
row++;
good = 0;
continue;
}
if (good)
{
--row;
row++;
frombacktrack = 1;
if(row == -1)
exit(1);
}
}
print(row, column);
--row;
row++;
frombacktrack = 1;
if(row==-1)
exit(1);
}
cout << endl << endl;
system("PAUSE");
}
Explanation / Answer
how about this? N-Queen's Problem #include #include #include #include #include void check(int, int, char [100][100]); void print(char [100][100]); int no_of_queens, queen = 2, flagrow = 0, flagcol = 0; int count = 1; char ch, response_row, response_col; int main(void) { int row, col, i; char board[100][100], response; clrscr(); printf(" @@ This is n-queen problem. Enter the number of queens(say n) and watch how computer places them in (n x n) matrix such that none can meet another moving along horizontally, vertically or digonally. "); printf(" Enter the number of queens : "); scanf("%d", &no_of_queens); if(no_of_queens > 23) { printf(" @@ Thought the program is OK for any queen value.But due the configuration of the output screen the output will be tranketed (A very large queen number may cause the system stack overflow). So it is highly recommended that you run the program with maximum queen number 23..."); printf(" Want to continue(Y/N)?"); fflush(stdin); scanf("%c", &response); if(toupper(response) == 'N') return (0); } else if(no_of_queens < 3) { printf("The number of Queen must be greater than 3."); getch(); return (0); } printf("Want a row number below the board(Y/N) : "); fflush(stdin); response_row = (char)getchar(); if(toupper(response_row) == 'Y') flagrow = 1; printf("Want a column number below the board(Y/N) : "); fflush(stdin); response_col = (char)getchar(); if(toupper(response_col) == 'Y') flagcol = 1; clrscr(); printf("M/c in work ! Please Wait..."); // This for-loop is used for checking all the columns of row 0 only... _setcursortype(_NOCURSOR); for(col = 0; col = 0 && jRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.