#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Các mảng kiểm tra cột và đường chéo
vector<bool> cols, diagA, diagB;
vector<vector<string>> results;
// Hàm kiểm tra xem có thể đặt hậu tại (x, y) không
bool isValid(int x, int y, int n) {
return !cols[y] && !diagA[n - 1 + x - y] && !diagB[x + y];
}
// Lưu kết quả khi tìm được cách đặt hậu
void saveResult(int n, vector<int>& pos) {
vector<string> board(n, string(n, '.'));
for (int i = 0; i < n; ++i) {
board[i][pos[i]] = 'Q';
}
results.push_back(board);
}
// Hàm quay lui để đặt hậu
void placeQueens(int row, int n, vector<int>& pos) {
for (int col = 0; col < n; ++col) {
if (isValid(row, col, n)) {
pos[row] = col; // Đặt hậu tại (row, col)
cols[col] = true; // Đánh dấu cột
diagA[n - 1 + row - col] = true; // Đánh dấu đường chéo chính
diagB[row + col] = true; // Đánh dấu đường chéo phụ
if (row == n - 1) {
saveResult(n, pos); // Nếu đặt đủ n hậu, lưu kết quả
} else {
placeQueens(row + 1, n, pos); // Đặt tiếp hậu ở hàng tiếp theo
}
// Hoàn tác để thử các khả năng khác
pos[row] = -1;
cols[col] = false;
diagA[n - 1 + row - col] = false;
diagB[row + col] = false;
}
}
}
int main() {
int n;
cin >> n;
// Khởi tạo các mảng kiểm tra
cols.resize(n, false);
diagA.resize(2 * n - 1, false);
diagB.resize(2 * n - 1, false);
vector<int> pos(n, -1);
// Gọi hàm quay lui
placeQueens(0, n, pos);
// In kết quả
cout << results.size() << endl; // Số cách đặt hậu
for (const auto& board : results) {
for (const string& row : board) {
cout << row << endl;
}
cout << endl; // Cách nhau bởi một dòng trống
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RyaW5nPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIEPDoWMgbeG6o25nIGtp4buDbSB0cmEgY+G7mXQgdsOgIMSRxrDhu51uZyBjaMOpbwp2ZWN0b3I8Ym9vbD4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4bqldSDEkcaw4budbmcgY2jDqW8gY2jDrW5oCiAgICAgICAgICAgIGRpYWdCW3JvdyArIGNvbF0gPSB0cnVlOyAgICAgICAgICAvLyDEkMOhbmggZOG6pXUgxJHGsOG7nW5nIGNow6lvIHBo4bulCgogICAgICAgICAgICBpZiAocm93ID09IG4gLSAxKSB7CiAgICAgICAgICAgICAgICBzYXZlUmVzdWx0KG4sIHBvcyk7IC8vIE7hur91IMSR4bq3dCDEkeG7pyBuIGjhuq11LCBsxrB1IGvhur90IHF14bqjCiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwbGFjZVF1ZWVucyhyb3cgKyAxLCBuLCBwb3MpOyAvLyDEkOG6t3QgdGnhur9wIGjhuq11IOG7nyBow6BuZyB0aeG6v3AgdGhlbwogICAgICAgICAgICB9CgogICAgICAgICAgICAvLyBIb8OgbiB0w6FjIMSR4buDIHRo4butIGPDoWMga2jhuqMgbsSDbmcga2jDoWMKICAgICAgICAgICAgcG9zW3Jvd10gPSAtMTsKICAgICAgICAgICAgY29sc1tjb2xdID0gZmFsc2U7CiAgICAgICAgICAgIGRpYWdBW24gLSAxICsgcm93IC0gY29sXSA9IGZhbHNlOwogICAgICAgICAgICBkaWFnQltyb3cgKyBjb2xdID0gZmFsc2U7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgY2luID4+IG47CgogICAgLy8gS2jhu59pIHThuqFvIGPDoWMgbeG6o25nIGtp4buDbSB0cmEKICAgIGNvbHMucmVzaXplKG4sIGZhbHNlKTsKICAgIGRpYWdBLnJlc2l6ZSgyICogbiAtIDEsIGZhbHNlKTsKICAgIGRpYWdCLnJlc2l6ZSgyICogbiAtIDEsIGZhbHNlKTsKICAgIHZlY3RvcjxpbnQ+IHBvcyhuLCAtMSk7CgogICAgLy8gR+G7jWkgaMOgbSBxdWF5IGx1aQogICAgcGxhY2VRdWVlbnMoMCwgbiwgcG9zKTsKCiAgICAvLyBJbiBr4bq/dCBxdeG6owogICAgY291dCA8PCByZXN1bHRzLnNpemUoKSA8PCBlbmRsOyAvLyBT4buRIGPDoWNoIMSR4bq3dCBo4bqtdQogICAgZm9yIChjb25zdCBhdXRvJiBib2FyZCA6IHJlc3VsdHMpIHsKICAgICAgICBmb3IgKGNvbnN0IHN0cmluZyYgcm93IDogYm9hcmQpIHsKICAgICAgICAgICAgY291dCA8PCByb3cgPDwgZW5kbDsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBlbmRsOyAvLyBDw6FjaCBuaGF1IGLhu59pIG3hu5l0IGTDsm5nIHRy4buRbmcKICAgIH0KCiAgICByZXR1cm4gMDsKfQ==