示例#1
0
void print(Constraints constraints) {
    int row = constraints.size() - 1;
    while (!constraints.empty()) {
        auto constraint = constraints.top();
        constraints.pop();

        std::cout << "Constraints[" << row-- << "]: ";
        for (auto c : constraint) {
            std::cout << c << " ";
        }
        std::cout << std::endl;
    }
}
示例#2
0
void guess(Queens& queens, Constraints& constraints) {
    auto row = queens.size();
    for (int column = 0; column < SIZE; ++column) {
        bool good = true;

        if (!constraints.empty() && constraints.top().find(column) != constraints.top().cend()) {
            good = false;
        } else {
            for (int other_row = 0; other_row < static_cast<int>(row); ++other_row) {
                if (queens[other_row] == column) {
                    good = false;
                    break;
                }
                if ((queens[other_row] + other_row) == (column + static_cast<int>(row)) ||
                    (queens[other_row] - other_row) == (column - static_cast<int>(row))) {
                    good = false;
                    break;
                }
            }
        }

        if (good) {
            queens.push_back(column);
            if (queens.size() > constraints.size()) {
                constraints.push(std::unordered_set<int>());
            }
            return;
        }
    }

    if (constraints.size() > queens.size()) {
        constraints.pop();
    }
    if (row == queens.size()) {
        constraints.top().insert(queens.back());
        queens.pop_back();
    }
}