TLE玄关!!

P1219 [USACO1.5] 八皇后 Checker Challenge

无痕 @ 2024-04-20 11:06:14

#include<iostream>
using namespace std;

int a[14], b[14 * 2], c[14 * 2];
int n, ans;
int record[14];

int print(){
    for (int i = 1; i <= n; i++)
        cout << record[i] << ' ';
    cout << endl;
}
void dfs(int now){
    if (now >= n + 1){ //当前放完了n个皇后,now放完了前n行 
        if (ans < 3) print();    //既然可以进入到这个if里,就表示符合条件 
        ans++; //统计数量 
        return;
    }
    for (int i = 1; i <= n; i++){
        if (a[i] == 0 && b[n + now - i] == 0 && c[now + i] == 0){ //只有符合条件的才会继续dfs 
            a[i] = 1;
            b[n + now - i] = 1;
            c[now + i] = 1;
            //打上标记

            record[now] = i;

            //记录放的列数 
            dfs(now + 1); //往下递归 

            a[i] = 0;
            b[n + now - i] = 0;
            c[now + i] = 0;
            //回溯 
        }
    }
}

int main(){
    cin >> n;
    dfs(1);
    cout << ans;
    return 0;
} 

本地没问题,提交全TLE是什么情况


by Fengjunning31 @ 2024-05-01 11:34:33

把int print()改成void print()即可。


|