为什么不开o2会超时 87分 开了ac

P1219 [USACO1.5] 八皇后 Checker Challenge

AVLw @ 2023-10-11 19:21:43

是哪里太慢了吗 感觉和正解也都差不多啊

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>

using namespace std;
const int N = 200;
int path[N]; int n, ans;

bool check(int i, int j) {
    for (int k = 0; k < i; k++) {
        if (j == path[k] || abs(i - k) == abs(j - path[k])) {
            return false;
        }
    }
    return true;
}

void dfs(int i) {
    if (i == n) {
        ans++;
        if (ans <= 3) {
            for (int i = 0; i < n; i++)
                printf("%d ", path[i]+1);
            printf("\n");
        }
        return;
    }
    for (int j = 0; j < n; j++) {
        if (check(i, j)) {
            path[i] = j;
            dfs(i + 1);
        }
    }

}

int main(){

     scanf("%d", &n);
     dfs(0);
     printf("%d\n", ans);

    return 0;
}

by Fire_Kylin @ 2023-10-11 19:27:49

你做的哪一题啊???我也试试。


by AVLw @ 2023-10-11 21:11:58

@884431LG 经典的八皇后 话说讨论区不会显示题目吗?


|