1 solutions

  • 0
    @ 2024-12-11 0:16:56

    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

    Information

    ID
    684
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By