cpp-algo-cases/chapter_backtracking/n_queens.cpp

66 lines
2.0 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: n_queens.cpp
* Created Time: 2023-05-04
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B7A8>n <20>ʺ<EFBFBD> */
void backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,
vector<bool> &diags1, vector<bool> &diags2) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>
if (row == n) {
res.push_back(state);
return;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int col = 0; col < n; col++) {
// <20><><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><C3B8>Ӷ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>ߺʹζԽ<CEB6><D4BD><EFBFBD>
int diag1 = row - col + n - 1;
int diag2 = row + col;
// <20><>֦<EFBFBD><D6A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>ߡ<EFBFBD><DFA1>ζԽ<CEB6><D4BD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>ڻʺ<DABB>
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// <20><><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD>ʺ<EFBFBD><CABA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڸø<DAB8><C3B8><EFBFBD>
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// <20><><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD>ø<EFBFBD><C3B8>ӻָ<D3BB>Ϊ<EFBFBD><CEAA>λ
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* <20><><EFBFBD><EFBFBD> n <20>ʺ<EFBFBD> */
vector<vector<vector<string>>> nQueens(int n) {
// <20><>ʼ<EFBFBD><CABC> n*n <20><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD> 'Q' <20><><EFBFBD><EFBFBD><EFBFBD>ʺ<EFBFBD><CABA><EFBFBD>'#' <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
vector<vector<string>> state(n, vector<string>(n, "#"));
vector<bool> cols(n, false); // <20><>¼<EFBFBD><C2BC><EFBFBD>Ƿ<EFBFBD><C7B7>лʺ<D0BB>
vector<bool> diags1(2 * n - 1, false); // <20><>¼<EFBFBD><C2BC><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>лʺ<D0BB>
vector<bool> diags2(2 * n - 1, false); // <20><>¼<EFBFBD>ζԽ<CEB6><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>лʺ<D0BB>
vector<vector<vector<string>>> res;
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* Driver Code */
int main() {
int n = 4;
vector<vector<vector<string>>> res = nQueens(n);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳<EFBFBD><EFBFBD><EFBFBD>Ϊ " << n << endl;
cout << "<EFBFBD>ʺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> " << res.size() << " <20><>" << endl;
for (const vector<vector<string>> &state : res) {
cout << "--------------------" << endl;
for (const vector<string> &row : state) {
printVector(row);
}
}
return 0;
}