簡體   English   中英

我的 N 皇后問題一直有效,直到 n = 5,我不知道為什么 C++

[英]My N Queen Problem works up until n = 5 and I don't know why C++

我對編碼還是很陌生,所以放輕松,但我正在研究 N Queen 問題,該問題需要為 n 大小的板提供大量解決方案,您可以在每行放置一個皇后。 我的代碼工作到 n=4,然后 n=5 輸出 11 和 output 0 之后的所有 n。-1s 出現在位置 [] 中直到 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;
}

希望這對您和您的努力有所幫助,運動!

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

真摯地,

麥克風

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM