I'm still fairly new to coding so take it easy but I'm working on the N Queen problem which calls for the amount of solutions for a board of n size where you can place a queen on every row. My code works up to n=4 and then n=5 outputs 11 and all n's after output 0. -1s appear in placements[] up to n=5 and then they aren't input into the array afterwards. I'm pretty clueless now so some help would be appreciated.
#include <iostream>
using namespace std;
//MAXROWS is same as MAXCOLUMNS or BOARDSIZE
const int MAXROWS = 20;
//queens' placements in each row
int placements[MAXROWS];
int n = 0;
int solutionsCount = 0;
bool canPlaceQueen(int row, int column)
{
for(int j = 0; j < row; j++)
if((placements[j] == column) // is there a queen in same column?
|| (abs(placements[j] - column) == abs(j-row))) // checks diagonals
return false; // column difference is same as row difference?
return true;
}
bool correctSolution()
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
if (placements[i] == placements[j])
{
return false;
}
}
for (int k = 0; k < n; k++)
{
if (placements[k] == -1)
{
return false;
}
}
return true;
}
void placeQueens(int row) {
//try to place queen in each column in current row
//then continue to explore: placeQueens(row+1)
//for each successful queen placement in final row, increment solutionsCount!
for (int i = 0; i < n; i++)
{
if (canPlaceQueen(row, i))
{
placements[row] = i;
if (row < n-1)
{
placeQueens(row+1);
}
}
else
{
placements[row] = -1;
}
if (correctSolution())
{
solutionsCount++;
cout << "add" << placements[0] << placements [1] << placements[2] << placements[3] << endl;
}
}
cout << "new" << placements[0] << placements [1] << placements[2] << placements[3] << endl;
for (int j = 0; j < n; j++)
{
placements[j] = 0;
}
}
int main() {
cout << "Enter the board size: ";
cin >> n;
placeQueens(0);
cout << "Number of solutions: " << solutionsCount << endl;
}
Hope this helps you and your endeavors, sport!
#include <iostream>
using namespace std;
//MAXROWS is same as MAXCOLUMNS or BOARDSIZE
const int MAXROWS = 20;
//queens' placements in each row
int placements[MAXROWS];
int n = 0;
int solutionsCount = 0;
bool canPlaceQueen(int row, int column)
{
for (int j = 0; j < row; j++)
if ((placements[j] == column) // is there a queen in same column?
|| (abs(placements[j] - column) == abs(j - row))) // checks diagonals
return false; // column difference is same as row difference?
return true;
}
void placeQueens(int row)
{
//try to place queen in each column in current row
//then continue to explore: placeQueens(row+1)
//for each successful queen placement in final row, increment solutionsCount!
if (row == n) //If the last row has been reached then there is a solution
{
solutionsCount++; //incrementing the solution count
return; //returning back
}
for (int column = 1; column <= n; column++) // To check all the columns from 1 to n
{
if (canPlaceQueen(row, column) == true)
{
placements[row] = column; //placing the queen in Row row on Column column
placeQueens(row + 1); //calling the next row
placements[row] = 0; //removing the queen from this column
}
}
}
int main()
{
cout << "Enter the board size: ";
cin >> n;
placeQueens(0);
cout << "Number of solutions: " << solutionsCount << endl;
}
Sincerely,
Mike
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.