我的 N Queen 问题一直持续到 n = 5,我不知道为什么是 C++

问题描述

我对编码仍然很陌生,所以放轻松,但我正在研究 N Queen 问题,该问题需要 n 大小的板的解决方数量,您可以在每一行放置一个皇后。我的代码最多可以工作到 n=4,然后 n=5 输出 11 和输出 0 之后的所有 n。-1s 出现在placements[] 中直到 n=5,然后它们不会输入到数组中。我现在很无知,所以希望得到一些帮助。

#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;
}
@H_502_4@

解决方法

希望这对您和您的努力有所帮助,运动!

#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;
}

真诚的

迈克