87分求调

P1219 [USACO1.5] 八皇后 Checker Challenge

CMY2013 @ 2024-07-04 08:03:24

#include<bits/stdc++.h>
using namespace std;
int n,a[13],o=0;

bool pan(int x,int y)
{
    for(int i=1;i<x;i++)
    {
        if(a[i]==y) return false;
        else if(abs(x-i)==abs(a[i]-y)) return false;
    }
    return true;
}

int num = 0;
void f(int m)
{
    for(int i=1;i<=n;i++)
    {
        if(pan(m,i))
        {
            a[m]=i;
            if(m==n)
            {
                if(num<3)
                {
                    for(int j=1;j<=n;j++)
                    {
                        cout<<a[j]<<' ';
                    }
                    cout<<endl;

                }
                num++;
                continue;
            }
            else f(m+1);
        }
    }
}
int main()
{
    cin>>n;
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
    memset(a,0,sizeof(a));
    f(1);
    cout << num << endl;
    return 0;
}

by 仙风道骨 @ 2024-07-04 08:58:06

数组开小了

(:


by CMY2013 @ 2024-07-07 11:03:02

谢了


|