Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
91924 sh25_shenpy 八皇后 C++ 通过 3 MS 248 KB 1089 2026-06-19 16:19:18

Tests(1/1):


#include <iostream> #include <cstring> using namespace std; const int N = 8; int col[N]; // col[i] 表示第 i 行皇后放在第几列 bool usedCol[N]; // 列是否被占用 bool usedDia1[2*N]; // 主对角线 i-j 偏移 bool usedDia2[2*N]; // 副对角线 i+j int cnt = 0; // 方案总数 // 输出一种方案 void print() { for (int i = 0; i < N; ++i) { cout << col[i] + 1; } cout << endl; } // 放置第 row 行 void dfs(int row) { if (row == N) { print(); return; } for (int c = 0; c < N; ++c) { int d1 = row - c + N; int d2 = row + c; if (!usedCol[c] && !usedDia1[d1] && !usedDia2[d2]) { col[row] = c; usedCol[c] = usedDia1[d1] = usedDia2[d2] = true; dfs(row + 1); usedCol[c] = usedDia1[d1] = usedDia2[d2] = false; } } } int main() { memset(usedCol, 0, sizeof(usedCol)); memset(usedDia1, 0, sizeof(usedDia1)); memset(usedDia2, 0, sizeof(usedDia2)); dfs(0); return 0; }


测评信息: