From f2a72b8e2d9e11200759980831ed0196840d902c Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 30 Jun 2021 19:33:34 +0800 Subject: [PATCH] =?UTF-8?q?843.=20n=E7=9A=87=E5=90=8E=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/6188239/ --- AcWing/843/843.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 AcWing/843/843.cpp diff --git a/AcWing/843/843.cpp b/AcWing/843/843.cpp new file mode 100644 index 00000000..22cf4740 --- /dev/null +++ b/AcWing/843/843.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int n; +bool col[20], dg[20], udg[20]; +char g[20][20]; + +void dfs(int u) { + if (u == n) { + for (int i = 0; i < n; i++) { + cout << g[i] << endl; + } + cout << endl; + return; + } + for (int i = 0; i < n; i++) { + if (!col[i] && !dg[u + i] && !udg[n - u + i]) { + g[u][i] = 'Q'; + col[i] = dg[u + i] = udg[n - u + i] = true; + dfs(u + 1); + col[i] = dg[u + i] = udg[n - u + i] = false; + g[u][i] = '.'; + } + } +} + +int main() { + cin >> n; + for(int i = 0 ; i < n ; i++) { + for(int j = 0 ; j < n ; j++) { + g[i][j] = '.'; + } + } + dfs(0); + return 0; +}