1 条题解

  • 0
    @ 2025-2-14 21:09:17

    C++ :

    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int N = 15;
    int n,a[N];
    bool b[N]={0},c[2*N]={0},d[2*N]={0};
    bool found(false);
    
    void search(int i){
    	if (i>n){
    		found=true; 
    		for (int j=1;j<n;j++) printf("%d ",a[j]);
    		printf("%d\n",a[n]);
    		return ;
    	}
    	for (int j=1;j<=n;j++)
    		if (!b[j] && !c[i+j] &!d[i-j+n]){
    			a[i] = j;
    			b[j] = c[i+j] = d[i-j+n] = true;
    			search(i+1);
    			b[j] = c[i+j] = d[i-j+n] = false;
    		}
    }
    
    int main(){	
    	cin>>n;
    	search(1);
    	if (!found) printf("no solute!\n"); 
    	return 0;
    }
    
    • 1

    信息

    ID
    669
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者