1 条题解

  • 0
    @ 2024-7-24 21:08:28
    #include <bits/stdc++.h>
    using namespace std;
    int ans[15];
    int n,i;
    bool has_ans=false;
    bool cheak(int row,int col)
    {
        for(i=1;i<row;i++)
        {
            if(ans[i]==col||(abs(row-i)==abs(col-ans[i])))
            {
                return false;
            }
        }
        return true;
    }
    void dfs(int row)
    {
        if(row>n)
        {
            has_ans=true;
            for(i=1;i<=n;i++)
            {
                cout<<ans[i]<<" ";
            }
            cout<<endl;
            return;
        }
        for(int col=1;col<=n;col++)
        {
            if(cheak(row,col))
            {
                ans[row]=col;
                dfs(row+1);
            }
        }
    }
    int main()
    {
        cin>>n;
        dfs(1);
        if(n==2||n==3)
        {
            cout<<"no solute!";
        }
        return 0;
    }
    
    • 1

    信息

    ID
    378
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    (无)
    递交数
    259
    已通过
    84
    上传者