Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图解决N皇后问题,但得到额外输出(输出错误)-奇怪的问题_C++_N Queens - Fatal编程技术网

C++ 试图解决N皇后问题,但得到额外输出(输出错误)-奇怪的问题

C++ 试图解决N皇后问题,但得到额外输出(输出错误)-奇怪的问题,c++,n-queens,C++,N Queens,我试图编写n queens问题的代码,但花了一整天的时间,结果是这样的,我确信主要逻辑是正确的,但也有一些额外的输出正在打印,请检查并帮助我 我的代码给出如下输出: 00132113032 00 12 13 21 01 13 20 32 00 12 13 21 01 13 20 32 02 10 23 31 但正确的输出应该是 01 13 20 32 02 10 23 31 对于4*4矩阵(棋盘)。 请帮我找出问题所在 #include <stdio.h> #include &l

我试图编写n queens问题的代码,但花了一整天的时间,结果是这样的,我确信主要逻辑是正确的,但也有一些额外的输出正在打印,请检查并帮助我

我的代码给出如下输出:

00132113032
00 12 13 21 01 13 20 32
00 12 13 21 01 13 20 32 02 10 23 31
但正确的输出应该是

01 13 20 32 02 10 23 31
对于4*4矩阵(棋盘)。 请帮我找出问题所在

#include <stdio.h>
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;


bool issafe(vector<vector<int>> chess,int row,int col)    // a function to check if queen is safe
{
    for (int i = row-1,j=col;i>=0;i--)
    {
        if(chess[i][j]==1)
        {
            return false;
        }
    }
    
    for( int i = row-1,j=col-1;i>=0 && j>=0;i--,j--)
    {
        if(chess[i][j]==1)
        {
            return false;
        }
    }
    
    for (int i =row-1,j=col+1;i>=0 && j<chess[0].size();i--,j++)
    {
        if(chess[i][j]==1)
        {
            return false;
        }
    }

    
    return true;
    
  
}


vector<vector<string>> printNqueens(int n,vector<vector<int>>& chess,vector<string>& stringsf, int row, vector<vector<string>>& fstring)
{
   if(row==chess.size())
   {
      fstring.push_back(stringsf);
      
       
      for (int i = 0; i <fstring.size(); i++) {         // just printing the final string to check the answer
        for (int j = 0; j < fstring[i].size(); j++)
            {cout << fstring[i][j] << " ";}
        cout << endl;}
        
   
    
    
       return fstring;
   }

    
    for (int col=0;col<chess[0].size();col++)
    {
        if(issafe(chess,row,col)==true)
        {
            chess[row][col]=1;
            string strrow=to_string(row);
            string strcol= to_string(col);
            
            stringsf.push_back(strrow+strcol);
           
            printNqueens(n, chess,stringsf,row+1,fstring);
            chess[row][col]=0;
        }
        
    }
    
    
    vector<vector<string>> temp1;
    return temp1;  // jsut returning for namesake, so that the ans can be printed .
}


int solveNQueens(int n) {
       
        vector<vector<int>> chess(n, vector<int>(n, 0));  //vector<vector<int>> chess[n][n];// filling up a vector of n*n with 0's
        vector<string> stringsf;   // stringosfar
        vector<vector<string>> fstring;  // finalstring
        int row = 0;
       printNqueens(n,chess,stringsf,row,fstring);
       return 5;  // returning somehting for namesake so that the ans can be printed for chekcing if the whole program works
      
      
      
    }

int main()
{
    solveNQueens(4);  // call to solveNqueens for 4 
}
#包括
#包括
#包括
#包括
使用名称空间std;
bool issafe(vector chess,int row,int col)//一个检查queen是否安全的函数
{
对于(int i=row-1,j=col;i>=0;i--)
{
if(chess[i][j]==1)
{
返回false;
}
}
对于(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--)
{
if(chess[i][j]==1)
{
返回false;
}
}

对于(int i=row-1,j=col+1;i>=0&&jShould
issafe
有4个循环,而不是3个?回溯时忘记还原
stringsf
。@molbdnilo具体怎么做?能否在代码中显示它?因为您在递归之前做了一次
推回
,返回之后的
回推
应该会撤销它。