poj 3239 N皇后问题可行解的构造

Description

The eight queens puzzle is the problem of putting eight chess queens on an 8 × 8 chessboard such that none of them is able to capture any other. The puzzle has been generalized to arbitrary n × n boards. Given n, you are to find a solution to the n queens puzzle.

Input

The input contains multiple test cases. Each test case consists of a single integer n between 8 and 300 (inclusive). A zero indicates the end of input.

Output

For each test case, output your solution on one line. The solution is a permutation of {1, 2, …, n}. The number in the ith place means the ith-column queen in placed in the row with that number.

Sample Input

8
0

Sample Output

5 3 1 6 8 2 4 7

//

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
/*
//一、当n mod 6 != 2 且 n mod 6 != 3时,有一个解为:


//


//2,4,6,8,...,n,1,3,5,7,...,n-1       (n为偶数)


//


//2,4,6,8,...,n-1,1,3,5,7,...,n       (n为奇数)


//


//(上面序列第i个数为ai,表示在第i行ai列放一个皇后;... 省略的序列中,相邻两数以2递增。下同)


//


//二、当n mod 6 == 2 或 n mod 6 == 3时,


//


//(当n为偶数,k=n/2;当n为奇数,k=(n-1)/2)


//


//k,k+2,k+4,...,n,2,4,...,k-2,k+3,k+5,...,n-1,1,3,5,...,k+1         (k为偶数,n为偶数)


//


//k,k+2,k+4,...,n-1,2,4,...,k-2,k+3,k+5,...,n-2,1,3,5,...,k+1,n       (k为偶数,n为奇数)


//


//k,k+2,k+4,...,n-1,1,3,5,...,k-2,k+3,...,n,2,4,...,k+1               (k为奇数,n为偶数)


//


//k,k+2,k+4,...,n-2,1,3,5,...,k-2,k+3,...,n-1,2,4,...,k+1,n           (k为奇数,n为奇数)
*/
int a[400];//从0开始0->n-1
void build(int n)
{
    int l=0;
    if(n%6!=2&&n%6!=3)
    {
        if(n&1)
        {
            for(int i=2;i<=n-1;i++,i++) a[l++]=i;
            for(int i=1;i<=n;i++,i++) a[l++]=i;
        }
        else
        {
            for(int i=2;i<=n;i++,i++) a[l++]=i;
            for(int i=1;i<=n-1;i++,i++) a[l++]=i;
        }
    }
    else
    {
        if(n&1)
        {
            int k=(n-1)/2;
            if(k&1)
            {
                for(int i=k;i<=n-2;i++,i++) a[l++]=i;
                for(int i=1;i<=k-2;i++,i++) a[l++]=i;
                for(int i=k+3;i<=n-1;i++,i++) a[l++]=i;
                for(int i=2;i<=k+1;i++,i++) a[l++]=i;
                a[l++]=n;
            }
            else
            {
                for(int i=k;i<=n-1;i++,i++) a[l++]=i;
                for(int i=2;i<=k-2;i++,i++) a[l++]=i;
                for(int i=k+3;i<=n-2;i++,i++) a[l++]=i;
                for(int i=1;i<=k+1;i++,i++) a[l++]=i;
                a[l++]=n;
            }
        }
        else
        {
            int k=n/2;
            if(k&1)
            {
                for(int i=k;i<=n-1;i++,i++) a[l++]=i;
                for(int i=1;i<=k-2;i++,i++) a[l++]=i;
                for(int i=k+3;i<=n;i++,i++) a[l++]=i;
                for(int i=2;i<=k+1;i++,i++) a[l++]=i;
            }
            else
            {
                for(int i=k;i<=n;i++,i++) a[l++]=i;
                for(int i=2;i<=k-2;i++,i++) a[l++]=i;
                for(int i=k+3;i<=n-1;i++,i++) a[l++]=i;
                for(int i=1;i<=k+1;i++,i++) a[l++]=i;
            }
        }
    }
}
int main()
{
    int n;//n*n棋盘
    while(scanf("%d",&n)==1&&n)
    {
        build(n);
        printf("%d",a[0]);
        for(int i=1;i<n;i++) printf(" %d",a[i]);printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值