好奇怪,答案应该都对上了啊,求大佬调调

P1219 [USACO1.5] 八皇后 Checker Challenge

Marisa_LV @ 2024-09-29 20:19:41

#include<iostream>
using namespace std;
int n, anw1 = 0;
int judge[100][100] = { 0 }, jl[100] = { 0 }, anw2[100][100]={ 0 };
void zen(int qi, int a) {
    int b;
    for (b = 1; b <= n; b++) {
        judge[qi][b]++;
    }
    judge[qi][a]--;
    for (b = 1; b <= n; b++) {
        judge[b][a]++;
    }
    judge[qi][a]--;
    int k = qi, l = a;
    while (k >= 1 && l >= 1) {
        judge[k--][l--]++;
    }
    judge[qi][a]--;
    k = qi, l = a;
    while (k <= n && l <= n) {
        judge[k++][l++]++;
    }
    judge[qi][a]--;
    k = qi, l = a;
    while (k <= n && l >= 1) {
        judge[k++][l--]++;
    }
    judge[qi][a]--;
    k = qi, l = a;
    while (k >= 1 && l <= n) {
        judge[k--][l++]++;
    }
}
void jian(int qi, int a) {
    int b;
    for (b = 1; b <= n; b++) {
        judge[qi][b]--;
    }
    judge[qi][a]++;
    for (b = 1; b <= n; b++) {
        judge[b][a]--;
    }
    judge[qi][a]++;
    int k = qi, l = a;
    while (k >= 1 && l >= 1) {
        judge[k--][l--]--;
    }
    judge[qi][a]++;
    k = qi, l = a;
    while (k <= n && l <= n) {
        judge[k++][l++]--;
    }
    judge[qi][a]++;
    k = qi, l = a;
    while (k <= n && l >= 1) {
        judge[k++][l--]--;
    }
    judge[qi][a]++;
    k = qi, l = a;
    while (k >= 1 && l <= n) {
        judge[k--][l++]--;
    }
}
void dfs(int qi) {
    if (qi == n + 1) {
            anw1++;
            for (int a = 1; a <= n; a++)
                anw2[anw1][a] = jl[a];
            return;
    }
        for (int a = 1; a <= n; a++) {
            if (judge[qi][a] == 0) {
                jl[qi] = a;
                zen(qi, a);
                dfs(qi + 1);
                jian(qi, a);
            }
        }
}
int main() {
    cin >> n;
    dfs(1);
    for (int a = 1; a <= min(3, anw1); a++) {
        for (int b = 1; b <= n; b++) {
            cout << anw2[a][b] << ' ';
        }
        cout << endl;
    }
    cout << anw1 << endl;
    return 0;
}

by Marisa_LV @ 2024-09-29 20:27:11

发现我写的好长啊,看到大佬的精简代码给我整自卑了都(哭)


|