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 ...
随机推荐
- ios开发——实用技术OC篇》倒计时实现的两种方法
倒计时实现的两种方法 timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: secondsCountDow ...
- mysql 分布式事务
php + mysql 分布式事务 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元: 事务应该具有4个属性:原子性.一致性.隔离性.持续性 原子性(atomicit ...
- lvs持久性工作原理和配置
作者: ISADBA|FH.CN 日期: // 转载请注明: 作者:ISADBA.COM|FH.CN BLOG:http://isadba.com 原文:http://isadba.com/?p=67 ...
- 在一个文件中有10G个整数,乱序排列,要求找出中位数
题目:在一个文件中有 10G 个整数,乱序排列,要求找出中位数.内存限制为 2G.只写出思路即可(内存限制为 2G的意思就是,可以使用2G的空间来运行程序,而不考虑这台机器上的其他软件的占用内存). ...
- 关于Service常驻内存不被清理的解决方法.
众所周知, Service是跑后台的. 但是有些Rom厂商把一键清理做的真是太好用了, 以至于一键清理变成了一种习惯, Service已经变的不再是Service了. 那为什么像诸如360, 微信, ...
- 通用PE u盘装Ghost Win7系统
http://www.tongyongpe.com/win7ghost.html 导读 通用pe工具箱是现在最老牌的的U盘装系统和维护电脑的专用工具之一,一键式制作.操作简单便捷,几乎100%支持所有 ...
- C#_自动测试2_连连看外挂
GUI自动化测试和做外挂的原理很相似,都是模拟用户的鼠标和键盘操作, 给自己的程序写自动化就是做测试,给别人的程序写自动化就是外挂了. 本文使用的技术也同样适用制作“对对碰”,"找茬&quo ...
- Index Generation
Index Generation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 230 Accepted: 89 Des ...
- Android(java)学习笔记103:Map集合的获取功能
package cn.itcast_01; import java.util.Collection; import java.util.HashMap; import java.util.Map; i ...
- java 类从哪个jar包加载的
<%@page contentType="text/html; charset=GBK"%><%@page import="java.security. ...
