0

我找到了一个解决方案或 nqueen 问题并尝试编译,但它在运行后编译并卡住。我找不到问题所在。

这是代码

主文件

#include <iostream>
#include <cmath>
#include <stack>
#include <vector>
#include "Queen.h"
using namespace std;
int main()
{
    int n;
    int row;
    int col;
    cout << "Enter the num of queens: " << endl;
    cin >> n;
    Queen one(1,1);
    stack<Queen> result;
    result.push(one);

    for (int i=0; i<n; i++) {
        Queen z(i,i);
        int c=z.getcol();
        if (one.hit(z,n)==true) {
            while(one.hit(z,n)==true) {
                int b=z.getcol();
                z.setcol((z.getcol()+1));
                one.hit(z,n);
            }
            result.push(z);
        } else {
            result.push(z);
        }

        for (int i=1; i<n; i++) {
            cout << "Number of Solution is "<< result.size() <<endl;
        }
        return 0;
    }
}

女王.h

#ifndef QUEEN_H
#define QUEEN_H

#include <iostream>
#include <cmath>
#include <stack>
#include <vector>
using namespace std;

class Queen
{
public:
    Queen();
    Queen(int row,int col);
    bool hit(Queen b, int size);
    int getrow();
    int getcol();
    int setcol(int x);

private:
    double row;
    double col;
};
#endif // QUEEN_H

女王.cpp

#include "Queen.h"

Queen::Queen()
{
    this->row=1;
    this->col=1;
}
Queen::Queen(int row, int col)
{
    this->row=1;
    this->col=1;
}
bool Queen::hit(Queen b, int size)
{
    int r1=row;
    double r2=b.getrow();
    int c1=col;
    double c2=b.getcol();
    int rmod=abs(r2-this->row);
    int cmod=abs(c2-this->col);
    //compare row
    if (row==b.getrow()) {
        return true;
    }
    //compare col
    if (col==b.getcol()) {
        return true;
    }
    //compare diag
    if (rmod==cmod) {
        return true;
    }

    else {
        return false;
    }
}
int Queen::getrow()
{
    return row;
}
int Queen::getcol()
{
    return col;
}
int Queen::setcol(int x)
{
    //int x;
    col=x;
    return x;
}

我认为所有语法看起来都很好。调试器说没有错误。我应该修复什么?

4

0 回答 0