Jason_LiDongJin @ 2023-02-10 18:43:39
丑陋的代码:
#include <bits/stdc++.h>
using namespace std;
bool board[15][15];
bool place[15][15];
bool ans[73713][15][15];
int k, cur = 0;
void Place_Queen(int x, int y) {
board[x][y] = 1;
for (int i = 1; i <= k; i++)
place[x][i] = 1;
for (int i = 1; i <= k; i++)
place[i][y] = 1;
for (int i = 1; i <= min(x, y); i++)
place[x - i][y - i] = 1;
for (int i = 1; i <= min(x, y); i++)
place[x - i + 1][y + 1] = 1;
}
void dfs(int cnt) {
if (cnt == k) {
memcpy(ans[++cur], board, sizeof(board));
}
for (int i = 1; i <= k; i++) {
bool temp[15][15];
bool t[15][15];
memcpy(temp, board, sizeof(board));
memcpy(t, place, sizeof(place));
if (place[cnt][i]) {
Place_Queen(cnt, i);
dfs(cnt + 1);
memcpy(board, temp, sizeof(temp));
memcpy(place, t, sizeof(t));
}
}
}
int main() {
cin >> k;
dfs(1);
for (int i = 1; i <= cur; i++) {
for (int j = 1; j <= k; j++) {
for (int m = 1; m <= k; m++) {
if (ans[i][j][m])
cout << m << " ";
}
}
cout << "\n";
}
return 0;
}