1 条题解

  • 0
    @ 2023-8-20 9:16:53
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,sum=0;
    bool mark[4][100];
    int res[100];
    
    void dfs(int i){
     if(i>n){
      ++sum;
      if(sum>3) return ;
      for(int j=1;j<=n;j++){
       if(j == 1) cout << res[j];
       else cout << " " << res[j];
      }
      cout<<endl;
     }
     else
     {
      for(int k=1;k<=n;k++){
       if( !mark[1][k]&&!mark[2][i+k]&&!mark[3][i-k+n] )
       {
        res[i]=k;
        mark[1][k]=true; //占领 k列
        mark[2][i+k]=true; 
        mark[3][i-k+n]=true; //占领两条对角线 
        dfs(i+1);
        mark[1][k]=false;
        mark[2][i+k]=false;
        mark[3][i-k+n]=false;
       }
      }
     }
    }
    
    int main()
    {
     IOS;
     cin>>n;
     dfs(1);
     cout<<sum<<endl;
     return 0; 
    }
    
    
    • 1

    信息

    ID
    1605
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    21
    已通过
    11
    上传者