现在的位置: 首页 > 综合 > 正文

BUAA 555 N皇后

2013年10月12日 ⁄ 综合 ⁄ 共 378字 ⁄ 字号 评论关闭
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 15;
int c[maxn];
int tot,n;
void search(int cur)
{
    int i,j;
    if(cur==n)
        tot++;
    else
    {
        for(i=0;i<n;i++)
        {
            int ok=1;
            c[cur]=i;
            for(j=0;j<cur;j++)
                if(c[cur]==c[j]||cur-c[cur]==j-c[j]|| cur+c[cur]==j+c[j])
                {
                    ok=0;
                    break;
                }
            if(ok)
                search(cur+1);
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        tot=0;
        memset(c,0,sizeof(c));
        search(0);
        printf("%d\n",tot);
    }
    return 0;
}

抱歉!评论已关闭.