N Queen问题的这种回溯方法不正确吗?

问题描述 投票:0回答:1

N Queen问题的行为不当,即使用long long int时给出了一些输出(尽管不正确),但如果是int,则给出了包含所有元素-1的板数组。

代码是:

  #include<iostream>
  #include<iomanip>
  #include<cstring>

  using namespace std;
  const int d=4;

 //fills 0 to all the positions that are unsafe due to queen placed at (x,y)

  void fill(int board[d][d],int x,int y){
    for (int i = 0; i < d; ++i)
    {
      //for row and column
      board[i][y]=0; 
      board[x][i]=0;
    }
    //for the diagonal following i-1 & j-1 pattern
    for(int i=x,j=y; i>=0 && j>=0 ; --i,--j){
      board[i][j]=0;
    }
    //for the diagonal following i+1 & j+1 pattern
    for(int i=x,j=y; i<d && j<d ; ++i,++j){
      board[i][j]=0;
    } 
    //for the diagonal following i-1 & j+1 pattern
    for(int i=x,j=y; i>=0 && j<d ; --i,++j){
      board[i][j]=0;
    }
    //for the diagonal following i+1 & j-1 pattern   
    for(int i=x,j=y; i<d && j>=0 ; ++i,--j){
      board[i][j]=0;
    }    
  }


 //fills -1, i.e. clears the positions that were filled earlier due to queen at (x,y)
  void unfill(int board[d][d],int x,int y){
    for (int i = 0; i < d; ++i)
    {
      board[i][y]=-1;
      board[x][i]=-1;
    }
    for(int i=x,j=y; i>=0 && j>=0 ; --i,--j){
      board[i][j]=-1;
    }
    for(int i=x,j=y; i<d && j<d ; ++i,++j){
      board[i][j]=-1;
    } 
    for(int i=x,j=y; i>=0 && j<d ; --i,++j){
      board[i][j]=-1;
    }
    for(int i=x,j=y; i<d && j>=0 ; ++i,--j){
      board[i][j]=-1;
    } 

  }


  void printboard(int board[d][d]){
    for (int i = 0; i < d; ++i)
    {
      for (int j = 0; j < d; ++j)
      {
        cout<<setw(3)<<board[i][j]<<" ";
      }cout<<endl;
    }
  } 


  bool solve(int board[d][d],int queenno,int x,int y){
    //Returns true if all the queens are placed properly
    if(queenno==4){
      return true;
    }

    for (int i = x; i < d; ++i)
    {
      for (int j = y; j < d; ++j)
      {
        // If the position is unoccupied or safe i.e. value is -1
        if(board[i][j]==-1){
          fill(board,i,j); //assigns 0 to the places that are unsafe due to queen
          board[i][j]=1;  // places the queen at i,j
          queenno++;

          bool ans=solve(board,queenno,i,j);
          if(ans==false){
            unfill(board,i,j);  // assigns -1, i.e. backtracks
            queenno--;
            board[i][j]=-1;    // clears the earlier position of the queen
          }
        }

      }
    }
    // If can't place all the queens return false
    if(x==d-1 && y==d-1 && queenno<4){
      return false;
    }

  }



  int main()
  {
    // Creating a board of size 4x4 and assigning -1 to all its element
    int board[d][d]; 
    memset(board,-1,sizeof(board));

    bool ans=solve(board,0,0,0);
    if(ans)
      printboard(board);
    else
      cout<<"Can't print";

    return 0;
  }

使用long long int时给出的输出(尽管错误)是>

  0  -1   0  -1 
  0   0   0  -1 
  1   0   0   0 
  0   0   1   0

[请告诉您回溯在哪里出错,以及为什么当使用int而不是long long int时,程序没有给出任何输出(即所有元素-1)。

N Queen问题的行为不当,即使用long long int时会给出一些输出(尽管不正确),但如果是int,则会给出包含所有元素-1的板数组。 ...

c++ c algorithm c++11 c++14
1个回答
0
投票

[为什么输出中有-1的原因是return true;函数末尾缺少solve。没有返回值,它是不确定的输出,因此会产生疯狂的结果。

© www.soinside.com 2019 - 2024. All rights reserved.