Poj 3239 Solution to the n Queens Puzzle
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
0Sample Output
5 3 1 6 8 2 4 7Source
POJ Monthly--2007.06.03, Yao, Jinyu
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的更多相关文章
- Pat1128:N Queens Puzzle
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 1128 N Queens Puzzle
1128 N Queens Puzzle (20 分) The "eight queens puzzle" is the problem of placing eight ch ...
- A1128. N Queens Puzzle
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...
- PAT A1128 N Queens Puzzle (20 分)——数学题
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...
- PAT甲级 1128. N Queens Puzzle (20)
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 1128 N Queens Puzzle[对角线判断]
1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...
- PAT 甲级 1128 N Queens Puzzle
https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...
- 1128 N Queens Puzzle (20 分)
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
- PAT_A1128#N Queens Puzzle
Source: PAT A1128 N Queens Puzzle (20 分) Description: The "eight queens puzzle" is the pro ...
随机推荐
- 微软数学库XNAMATH(DirectXMath)
这篇文章只是对着MSDN文档的一些吐槽和总结记录,个人笔记之类的 运行库与头文件 老实说,这个数学库微软还是更像蛮频繁的,我这里有的最早版本是伴随DX9的,在这个头文件里面 最近在使用DXUT,顺便也 ...
- 使用SCNetworkReachability判断网络是否连接
先来看一下整个方法 - (BOOL)isConnectionAvailable { //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态 struct sockaddr_in zeroAdd ...
- use selenium in scrapy webdriver
https://pypi.python.org/pypi/selenium from selenium import webdriver from selenium.webdriver.common. ...
- ganglia监控hadoop2.0配置方法
ganglia监控hadoop2.0配置方法前提:hadoop2.0集群已安装ganglia监控工具第一步:Hadoop用户登录集群每一个节点,修改文件:vi /opt/hadoop-2.0.0-cd ...
- 给jdk写注释系列之jdk1.6容器(3)-Iterator设计模式
前面讲了两种List,一种基于数组实现的ArrayList,一种基于链表实现的LinkedList,这两种list是我们工作中最常用到的List容器.当然数组和链表也是两种常见的基本数据结构,其他基本 ...
- javaweb学习总结二(静态导入、自动拆装箱、增强for与可变参数)
一:静态导入语法:import static 导入类.成员变量|成员方法那么在代码中可以直接使用变量或者方法,而不需要通过类名.来调用 静态导入: import static java.lang.Sy ...
- Adobe Edge Animate –获取鼠标位置及跟随鼠标功能实现
Adobe Edge Animate –获取鼠标位置及跟随鼠标功能实现 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 在网络上浏览有关Edge相关问题的时 ...
- [改善Java代码] 谨慎包装类型的大小比较
建议27:谨慎包装类型的大小比较 基本数据类型比较大小木有问题,不过其对应的包装类型大小比较就需要注意了.看如下代码: public class Client { public static void ...
- poj 3544 Journey with Pigs
Journey with Pigs Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3004 Accepted: 922 ...
- JavaScript “\”替换成 “\\”
str=str.replace(/\\/g,'\\\\');