1.Link:

http://poj.org/problem?id=3239

2.Content:

Solution to the n Queens Puzzle
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 3459   Accepted: 1273   Special Judge

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

Source

3.Method:

一开始用8皇后的方法,发现算不出来。

只能通过搜索,可以利用构造法,自己也想不出来构造,所以直接套用了别人的构造公式

感觉没啥意义,直接就用别人的代码提交了,也算是完成一道题目了

构造方法:

http://www.cnblogs.com/rainydays/archive/2011/07/12/2104336.html

一、当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为奇数)

第二种情况可以认为是,当n为奇数时用最后一个棋子占据最后一行的最后一个位置,然后用n-1个棋子去填充n-1的棋盘,这样就转化为了相同类型且n为偶数的问题。

若k为奇数,则数列的前半部分均为奇数,否则前半部分均为偶数。

4.Code:

http://blog.csdn.net/lyy289065406/article/details/6642789?reload

 /*代码一:构造法*/

 //Memory Time
//188K 16MS #include<iostream>
#include<cmath>
using namespace std; int main(int i)
{
int n; //皇后数
while(cin>>n)
{
if(!n)
break; if(n%!= && n%!=)
{
if(n%==) //n为偶数
{
for(i=;i<=n;i+=)
cout<<i<<' ';
for(i=;i<=n-;i+=)
cout<<i<<' ';
cout<<endl;
}
else //n为奇数
{
for(i=;i<=n-;i+=)
cout<<i<<' ';
for(i=;i<=n;i+=)
cout<<i<<' ';
cout<<endl;
}
}
else if(n%== || n%==)
{
if(n%==) //n为偶数
{
int k=n/;
if(k%==) //k为偶数
{
for(i=k;i<=n;i+=)
cout<<i<<' ';
for(i=;i<=k-;i+=)
cout<<i<<' ';
for(i=k+;i<=n-;i+=)
cout<<i<<' ';
for(i=;i<=k+;i+=)
cout<<i<<' ';
cout<<endl;
}
else //k为奇数
{
for(i=k;i<=n-;i+=)
cout<<i<<' ';
for(i=;i<=k-;i+=)
cout<<i<<' ';
for(i=k+;i<=n;i+=)
cout<<i<<' ';
for(i=;i<=k+;i+=)
cout<<i<<' ';
cout<<endl;
}
}
else //n为奇数
{
int k=(n-)/;
if(k%==) //k为偶数
{
for(i=k;i<=n-;i+=)
cout<<i<<' ';
for(i=;i<=k-;i+=)
cout<<i<<' ';
for(i=k+;i<=n-;i+=)
cout<<i<<' ';
for(i=;i<=k+;i+=)
cout<<i<<' ';
cout<<n<<endl;
}
else //k为奇数
{
for(i=k;i<=n-;i+=)
cout<<i<<' ';
for(i=;i<=k-;i+=)
cout<<i<<' ';
for(i=k+;i<=n-;i+=)
cout<<i<<' ';
for(i=;i<=k+;i+=)
cout<<i<<' ';
cout<<n<<endl;
}
}
}
}
return ;
}

Poj 3239 Solution to the n Queens Puzzle的更多相关文章

  1. Pat1128:N Queens Puzzle

    1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...

  2. PAT 1128 N Queens Puzzle

    1128 N Queens Puzzle (20 分)   The "eight queens puzzle" is the problem of placing eight ch ...

  3. A1128. N Queens Puzzle

    The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...

  4. PAT A1128 N Queens Puzzle (20 分)——数学题

    The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...

  5. PAT甲级 1128. N Queens Puzzle (20)

    1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...

  6. PAT 1128 N Queens Puzzle[对角线判断]

    1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...

  7. PAT 甲级 1128 N Queens Puzzle

    https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...

  8. 1128 N Queens Puzzle (20 分)

    The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...

  9. PAT_A1128#N Queens Puzzle

    Source: PAT A1128 N Queens Puzzle (20 分) Description: The "eight queens puzzle" is the pro ...

随机推荐

  1. 架构师之路(39)---IoC框架

    1 IoC理论的背景     我们都知道,在採用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,全部的对象通过彼此的合作,终于实现系统的业务逻辑.   图1:软件系统中耦合的对象 假设 ...

  2. Ubuntu下多服务器 Rsync同步镜像服务配置

    主服务器:192.168.5.13_ubuntu 从服务器:192.168.5.11_centos ================== 1> 在两台主机上分别安装rsync========== ...

  3. iOS开发——笔记篇&关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结

    关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结 一:Plist读取 /************************************ ...

  4. 关于jquery ID选择器的一点看法

    最近看到一道前端面试题: 请优化selector写法:$(".foo div#bar:eq(0)") 我给出的答案会是: 1. $("#bar") 2.  $( ...

  5. mysql 5.7.12 新增 X plugin 详解

     https://dev.mysql.com/doc/refman/5.7/en/document-store.html   原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息 ...

  6. hadoop实例---多表关联

    多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息.如下 输入的是两个文件,一个代表工厂表,包含工厂名列和地址编号列:另一个代表地址表,包含地址名列和地址编号列.要求从 ...

  7. CSS: Float a div on another div, Ex: Text caption on picture

    <style type="text/css"> .figure { width: 316px; height: 205px; display: block; borde ...

  8. LAMP平台搭建详解

    准备工作 安装编译工具 # yum -y install gcc # yum -y install gcc-c++ 如果系统之前已经安装有rpm包的mysql和apache,那么可以: #servic ...

  9. [改善Java代码]异步运算考虑使用Callable接口

    多线程有两种实现方式: 一种是实现Runnable接口,另一种是继承Thread类,这两种方式都有缺点,run方法没有返回值,不能抛出异常(这两个缺点归根到底是Runable接口的缺陷,Thread也 ...

  10. python中那些双下划线开头得函数和变量--转载

    Python中下划线---完全解读     Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用'from module import *'导入 __xxx__ 系统定义名字 __x ...