1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 10;
LL ans = 0; LL n = 8; LL tt = 0; char c[10][10]; struct node { LL x, y; }a[N];
bool st[10];
void dfs(int u) {
if (u > n) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << c[i][j]; } cout << endl; } cout << endl; //ans++; return; }
for (int i = 1; i <= n; i++) { if (!st[i]) { bool ok = true; LL x = i, y = u; for (int j = 1; j < u; j++) { LL xx = a[j].x,yy = a[j].y; if (abs(yy-y) == abs(x-xx)) { ok = false; break; } }
if (ok) { c[i][u] = 'Q'; st[i] = true; a[++tt] = { x,y }; dfs(u + 1); st[i] = false; c[i][u] = '.'; tt--; } else continue; } }
}
void solve() { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c[i][j] = '.'; dfs(1); //cout << ans<<endl; }
int main() { LL t = 1; while (t--) solve(); return 0; }
|