不开氧气会tle最后一个,肿么办
查看原帖
不开氧气会tle最后一个,肿么办
1276544
luoguerepp楼主2024/3/14 13:20
#include<iostream>

using namespace std;

const int N = 30;

int n;
int ans[N],cnt;
bool st[N][N];

bool check(int x,int y){
    for(int i = 1; i <= x; ++i)
        if(st[i][y])    return false;
    
    for(int i = x - 1,j = y - 1; i >= 1 && j >= 1; --i,--j)
        if(st[i][j])    return false;
    
    for(int i = x - 1,j = y + 1; i >= 1 && j <= n; --i,++j)
        if(st[i][j])    return false;
    
    return true;
}
void dfs(int x){
    if(x > n){
    	cnt++;
    	if(cnt > 3)	return;
        for(int i = 1; i < x; ++i) printf("%d ",ans[i]);
        printf("\n");
        return;
    }
    
    for(int j = 1; j <= n; ++j)
        if(check(x,j)){
           st[x][j] = true;
           ans[x] = j;
           dfs(x + 1);
           st[x][j] = false;
        }
}
int main(){
    scanf("%d",&n);
    dfs(1);
    printf("%d",cnt);
    return 0;
}
2024/3/14 13:20
加载中...