求助,开了O2AC,不开最后一个点TLE
查看原帖
求助,开了O2AC,不开最后一个点TLE
630442
franxx112楼主2022/10/13 19:56

大佬帮忙看看谢谢啦

#include<iostream>
#include<cstdio>
#define Row ((x + n - 1) / n)
#define List (x - Row*n+n)
#define Lslash (Row+List-1)
#define Rslash (n-List+Row)
using namespace std;
bool row[20], list[20], lslash[30], rslash[30], finish = 0;
int n, ans,a[100];
bool b(int x) {
    return !(row[Row] || list[List] || lslash[Lslash] || rslash[Rslash]);
}
void dfs(int x) {
    if (x > n * n) {
        ans++;
        finish = 1;
        if (ans <= 3){
			for(int i=1;i<=n;i++){
				printf("%d ",a[i]);
			}
			printf("\n");
		};
        return;
    }
    if (b(x)) {
        row[Row] = 1;
        list[List] = 1;
        lslash[Lslash] = 1;
        rslash[Rslash] = 1;
		a[Row]=List;
        for (int i = 1; i <= n; i++) {
        
            dfs(Row * n + i);
            if (finish) {
                finish = 0;
                break;
            }
        }
        list[List] = 0;
        lslash[Lslash] = 0;
        rslash[Rslash] = 0;
        row[Row] = 0;
    }
}
int main() {
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
        dfs(i);
    cout << ans;
    return 0;
}
2022/10/13 19:56
加载中...