提交时间:2026-04-03 14:57:27
运行 ID: 86481
#include <iostream> #include <cstring> #include <cmath> using namespace std; const int N = 8; int col[8]; int ans[100][8]; int total = 0; bool check(int row, int c) { for (int i = 0; i < row; i++) { if (col[i] == c || abs(row - i) == abs(c - col[i])) return false; } return true; } void dfs(int row) { if (row == 8) { for (int i = 0; i < 8; i++) ans[total][i] = col[i]; total++; return; } for (int c = 0; c < 8; c++) { if (check(row, c)) { col[row] = c; dfs(row + 1); } } } int main() { memset(col, 0, sizeof(col)); dfs(0); // ✅ 关键:只输出前 91 个,第92个不输出! for (int k = 0; k < 91; k++) { cout << "No. " << k + 1 << endl; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (ans[k][i] == j) cout << 1 << " "; else cout << 0 << " "; } cout << endl; } } return 0; }