#include <iostream> #include <cstring> #include <algorithm>using namespace std; const int N = 10; int n; int col[N], dg[N + N], rdg[N + N]; // 列占用// 正对角线占用// 副对角线占用 char table[N][N]; // 棋盘void dfs(int index) {if(index == n) // 结束条件{for (int i = 0; i < n; i ++ ){for (int j = 0; j < n; j ++ )cout << table[i][j];cout << endl;}cout << endl; return; }for (int j = 0; j < n; j ++ ){// n - index = b + j + 1 正对角线 (b会为负,要再加上n)// n - index = b - (j + 1) 副对角线 if(!col[j] && !dg[n - index - j - 1 + n] && !rdg[n - index + j + 1]){col[j] = dg[n - index - j - 1 + n] = rdg[n - index + j + 1] = 1;table[index][j] = 'Q';dfs(index + 1);table[index][j] = '.'; // 搜索完恢复col[j] = dg[n - index - j - 1 + n] = rdg[n - index + j + 1] = 0;}}} int main() {cin >> n;for (int i = 0; i < n; i ++ )for (int j = 0; j < n; j ++ )table[i][j] = '.';dfs(0);return 0; }