人間夜行

一切の有為の法 夢幻泡影の如し

RQNOJ 589 N皇后加强版

| 评论

//递归法。所幸规模小。
#include <stdio.h>
int n;
int sum=0;
int lie[14]={0};
int zh[14]={0};
int zzx[30]={0};
int zfx[30]={0};
void try(int i)
{
        if(i>n)
        {
                if(sum<3)
                {
                        int s;
                        for(s=1;s<n;s++) printf("%d ",lie[s]);
                        printf("%d\n",lie[s]);
                }
                sum++;
        }
        else
        {
                int r;
                for(r=1;r<=n;r++)
                        if(zh[r]==0&&zzx[i+r]==0&&zfx[i-r+n]==0)
                        {
                                lie[i]=r;
                                zh[r]=1;
                                zzx[i+r]=1;
                                zfx[i-r+n]=1;
                                try(i+1);
                                zh[r]=0;
                                zzx[i+r]=0;
                                zfx[i-r+n]=0;
                        }
        }
}

int main()
{         scanf("%d",&n);
        try(1);
        printf("%d",sum);
        return 0;
}

评论