poj3239 Solution to the n Queens Puzzle (n皇后问题)

八皇后问题(Eight Queens Puzzle) 八皇后问题 八皇后问题是由国际象棋棋手马克斯·贝瑟尔(Max Bezzel)于1848年提出的:在8×8格的国际象棋棋盘上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。 八皇后问题可以推广为更一般的nnn皇后问题:在n×nn×nn×n的棋盘上摆放nnn个皇后,使其不能互相攻击(仅当n=2n=2n=2和n=3n=3n=3时无解)。 解题... 阅读详情

Solution to the n Queens Puzzle
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 3494 Accepted: 1285 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

解题思路:

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

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

 

(上面有六条序列。一行一个序列,中括号是我额外加上的,方便大家辨认子序列,子序列与子序列之间是连续关系,无视中括号就可以了。第i个数为ai,表示在第i行ai列放一个皇后;... 省略的序列中,相邻两数以2递增。)

参考代码:

#include<iostream>
#include<cmath>
using namespace std;

int main(int i)
{
	int n;  //皇后数
	while(cin>>n)
	{
		if(!n)
			break;

		if(n%6!=2 && n%6!=3)
		{
			if(n%2==0)  //n为偶数
			{
				for(i=2;i<=n;i+=2)
					cout<<i<<' ';
				for(i=1;i<=n-1;i+=2)
					cout<<i<<' ';
				cout<<endl;
			}
			else   //n为奇数
			{
				for(i=2;i<=n-1;i+=2)
					cout<<i<<' ';
				for(i=1;i<=n;i+=2)
					cout<<i<<' ';
				cout<<endl;
			}
		}
		else if(n%6==2 || n%6==3)
		{
			if(n%2==0)  //n为偶数
			{
				int k=n/2;
				if(k%2==0)  //k为偶数
				{
					for(i=k;i<=n;i+=2)
						cout<<i<<' ';
					for(i=2;i<=k-2;i+=2)
						cout<<i<<' ';
					for(i=k+3;i<=n-1;i+=2)
						cout<<i<<' ';
					for(i=1;i<=k+1;i+=2)
						cout<<i<<' ';
					cout<<endl;
				}
				else  //k为奇数
				{
					for(i=k;i<=n-1;i+=2)
						cout<<i<<' ';
					for(i=1;i<=k-2;i+=2)
						cout<<i<<' ';
					for(i=k+3;i<=n;i+=2)
						cout<<i<<' ';
					for(i=2;i<=k+1;i+=2)
						cout<<i<<' ';
					cout<<endl;
				}
			}
			else   //n为奇数
			{
				int k=(n-1)/2;
				if(k%2==0)  //k为偶数
				{
					for(i=k;i<=n-1;i+=2)
						cout<<i<<' ';
					for(i=2;i<=k-2;i+=2)
						cout<<i<<' ';
					for(i=k+3;i<=n-2;i+=2)
						cout<<i<<' ';
					for(i=1;i<=k+1;i+=2)
						cout<<i<<' ';
					cout<<n<<endl;
				}
				else  //k为奇数
				{
					for(i=k;i<=n-2;i+=2)
						cout<<i<<' ';
					for(i=1;i<=k-2;i+=2)
						cout<<i<<' ';
					for(i=k+3;i<=n-1;i+=2)
						cout<<i<<' ';
					for(i=2;i<=k+1;i+=2)
						cout<<i<<' ';
					cout<<n<<endl;
				}
			}
		}
	}
	return 0;
}



【仓颉 + 鸿蒙 + AI Agent】CangjieMagic框架(3):Agent请求和响应 CangjieMagic框架:使用华为仓颉编程语言编写,专门用于开发AI Agent,支持鸿蒙、Windows、macOS、Linux等系统。这篇文章剖析一下 CangjieMagic 框架中的 AgentRequest 和 AgentResponse。 阅读详情

相关推荐

Linux 音频驱动(六) ALSA音频驱动之PCM Write数据传递过程

目录1. 前言2. PCM Data Flow3. 总结 1. 前言 本文,我们将以回放(Playback,播放音频)为例,讲解PCM Data是如何从用户空间到内核空间,最后传递到Codec。 在Linux 音频驱动(一) ASoC音频框架简介中,我们给出了回放(Playback)PCM数据流示意图: 对于Linux来说,由于分为 user space 和kernel space,而且两者之间不能随便互相访问。因此用户如果播放音频,则需要调用copy_from_user()将用户数据从user spa

_Modest_的博客 1万+

POJ 3239 Solution to the n Queens Puzzle

Solution to the n Queens Puzzle Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3469   Accepted: 1275   Special Judge Description The eight queens p

Nightonke's Blog 1031

Simulink中通过MEX与MinGW实现C/C++结构体变量的高效导入

本文详细介绍了在Simulink中通过MEX工具链与MinGW编译器,高效导入C/C++结构体变量的完整流程。文章从需求背景出发,逐步讲解了MinGW环境配置、.h文件编写规范、核心导入命令Simulink.importExternalCTypes的使用,以及如何在MATLAB Function模块和总线创建器中调用自定义结构体,旨在实现模型与代码的无缝对齐,为后续仿真和代码生成奠定坚实基础。

weixin_29056781的博客 353

POJ3239-Solution to the n Queens Puzzle

全解题报告索引目录 -> 【北大ACM – POJ试题分类】 转载请注明出处:http://exp-blog.com ------------------------------------------------------------------------- N皇后问题,由于n的规模较大,可使用构造法解题。 构造法原理: 英文原文(原作者E.J.Ho...

ζёСяêτ - 小優YoU 4708

抖音短视频数据抓取实战系列(一)——模拟器性能对比与优化配置

本文是抖音短视频数据抓取实战系列的开篇,重点探讨了模拟器选型与优化配置。通过对比雷电、夜神、逍遥三大主流模拟器在性能、抖音兼容性及抓包友好度上的表现,文章指出雷电模拟器因其稳定的桥接网络模式和良好的兼容性,是数据抓取的优选。并提供了雷电模拟器从安装、网络桥接到系统优化的详细配置指南,旨在帮助读者构建稳定高效的抓取环境。

weixin_29230901的博客 252

西瓜书第二章——模型评估与选择(十折交叉验证/查准查全F1)

第二章——模型评估与选择

xsddys的博客 957

Eight queens puzzle 八皇后

From Wikipedia, the free encyclopedia The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture any other using the standard...

weixin_34292924的博客 206

在win10-wsl2(ubuntu)中安装jlink

在 官方下载linux版本的deb包拷贝到ubuntu的/home目录下(其他目录也可以),进行安装然后提示很多依赖缺失的错误,此时执行自动安装需要的依赖包再次重新安装就可以啦查看是否安装成功,输入JLink 再按下两次Table键,是否如下所示这样表示安装成功了。

yaq_30401的博客 3151

POJ3239《Solution to the n Queens Puzzle》方法:构造公式法

题目大意:n(830时,传统方法不再使用。所以,网上有巧妙的构造公式方法: 一、当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为奇数) 二、当n mod 6 == 2 或 n mod 6 =

相门码农的专栏 872

28-Halcon-- union_object_model_3d函数功能(用于3D模型融合)

Halcon算子union_object_model_3d是专业的3D模型融合工具,主要用于多视图拼接、部件组装和多源数据融合。该算子支持点云、网格和参数化模型输入,提供多种融合算法选择,包括基础点云拼接('points_cloud')、网格融合('mesh')和精确CAD布尔运算('cad')。工业应用涵盖逆向工程扫描拼接、汽车部件虚拟装配等场景,支持性能优化如空间分区和流式处理。算子还解决颜色属性融合、法向量保留等特殊需求,并提供拼接误差分析、拓扑验证等质量评估手段。最佳实践包含动态场景重建工作流和CA

weixin_45590420的博客 1057

【K8S系列】Kubernetes Pod节点CrashLoopBackOff 状态及解决方案详解【已解决】

在 Kubernetes 中,Pod 的状态为 **`CrashLoopBackOff`** 表示某个容器在启动后崩溃,Kubernetes > 尝试重启该容器,但由于持续崩溃,重启的间隔时间逐渐增加。下面将详细介绍 `CrashLoopBackOff > `状态的原因、解决方案及相关命令的输出解释。

颜淡慕潇 8385

poj 3239 Solution to the n Queens Puzzle n皇后问题的构造解法

#include using namespace std; int n; int main() { while(scanf("%d",&n)==1&&n){ if(n%6!=2&&n%6!=3){ for(int i=2;i<=n;i+=2) printf("%d ",i); for(int i=1;i<=n;i+=2) printf("%d ",i);

sepNINE的专栏 673

手把手教你学Simulink--基于Simulink的高增益DC-DC变换器仿真建模

特点高电压增益:可以在不使用变压器的情况下实现较大的电压升压比。适用于低电压源:如太阳能电池板、燃料电池等提供的低压直流电源。效率问题:设计时需特别注意提高效率,减少能量损失。挑战电路复杂性:通常涉及多个电感和电容,以及复杂的开关模式。元件选择:合适的元件参数对于达到理想性能至关重要。控制策略:确保高效稳定运行的同时,适应负载变化。首先明确要模拟的任务内容。实现特定的电压增益比(例如从24V升至380V)。根据预期的输入输出电压确定合适的电路拓扑和元件值。

MHD0815的博客 176

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

西门求雪的专栏 412

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

POJ3239 Solution to the n Queens Puzzle

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

javays1的专栏 486

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

POJ 3239 Solution to the n Queens Puzzle 犀利利的构造公式

Solution to the n Queens Puzzle Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 2897   Accepted: 1067   Special Judge Description The eight queens puzzle

1197

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

POJ 3239【Solution to the n Queens Puzzle】

题目链接 思路: 第一次做m皇后问题,用了深搜,TLE。注意这里的深搜可以逐列搜索,而深度也无需记录,因为可以用列数充当深度。dfs() 只有列数一个参数。并且这里的 flip() 比较特殊,我使用的方法是记录一个三维的标记数组,第一维代表已经填充的皇后造成的标记,所以每次翻转要用 memcpy(rem[y] , rem[y-1] , sizeof(rem[y-1])); 以便回溯时消除标...

Masker 222

POJ 3239 -- Solution to the n Queens Puzzle

Solution to thenQueens Puzzle Time Limit:1000MS Memory Limit:131072K Total Submissions:3872 Accepted:1419 Special Judge Description The eight queens puzzle ...

weixin_30666401的博客 177

BZOJ_P3101 N皇后/POJ_P3239 Solution to the n Queens Puzzle(N皇后构造)

BZOJ传送门 POJ传送门Time Limit: 10 Sec Memory Limit: 128 MBSec Special Judge Submit: 147 Solved: 72 [Submit][Status][Discuss] Description n*n的棋盘,在上面摆下n个皇后,使其两两间不能相互攻击…Input 一个数nOutput 第i行表示在第i行第几列放

BeiYu 374

Solution to the n Queens Puzzle

给你一个n*n的矩阵,现在输出一个解8&lt;=n&lt;=30;普通的dfs会超时,在这里直接举例找数学规律。一、当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为奇数)二、当n mod 6 == 2 或 ...

Cworld2017的博客 316

PAT甲级1128

1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The "eight queens puzzle" is the problem o

湛蓝的天空 741

PAT - 甲级 - 1128. N Queens Puzzle (20) (8皇后问题变形)

题目描述: The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the s

i逆天耗子 2233

PAT--1128. N Queens Puzzle

The “eight queens puzzle” is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, c

许少y 1289
上一篇: 二叉树的遍历
下一篇: poj1047 Round and Round We Go
JinbaoSite0144
博客等级 码龄12年 231粉丝 157原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值