PAT甲级——A1128 N Queens Puzzle【20】
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general N queens problem of placing N non-attacking queens on an N×N chessboard. (From Wikipedia - "Eight queens puzzle".)
Here you are NOT asked to solve the puzzles. Instead, you are supposed to judge whether or not a given configuration of the chessboard is a solution. To simplify the representation of a chessboard, let us assume that no two queens will be placed in the same column. Then a configuration can be represented by a simple integer sequence (, where Qi is the row number of the queen in the i-th column. For example, Figure 1 can be represented by (4, 6, 8, 2, 7, 1, 3, 5) and it is indeed a solution to the 8 queens puzzle; while Figure 2 can be represented by (4, 6, 7, 2, 8, 1, 9, 5, 3) and is NOT a 9 queens' solution.
Figure 1 | Figure 2 |
Input Specification:
Each input file contains several test cases. The first line gives an integer K (1). Then K lines follow, each gives a configuration in the format "N Q1 Q2 ... QN", where 4 and it is guaranteed that 1 for all ,. The numbers are separated by spaces.
Output Specification:
For each configuration, if it is a solution to the N queens problem, print YES
in a line; or NO
if not.
Sample Input:
4
8 4 6 8 2 7 1 3 5
9 4 6 7 2 8 1 9 5 3
6 1 5 2 6 4 3
5 1 3 5 2 4
Sample Output:
YES
NO
NO
YES
#include <iostream>
#include <vector>
using namespace std;
int queen[];
int main()
{
int k, n, a;
cin >> k;
while (k--)
{
fill(queen, queen + , );
cin >> n;
bool res = true;
for (int i = ; i <= n; ++i)
{
cin >> queen[i];//新一列存入queen
for (int t = ; t < i; ++t)//判断前i-1列的queen是不是在同一行
{
if (queen[i] == queen[t] || abs(queen[i] - queen[t]) == abs(i - t))//是否存在相同行,和第t列的斜线位置
{
res = false;
break;
}
}
}
cout << (res == true ? "YES" : "NO") << endl;
}
return ;
}
PAT甲级——A1128 N Queens Puzzle【20】的更多相关文章
- 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 (20) 【STL】
题目链接 https://www.patest.cn/contests/pat-a-practise/1128 思路 可以 对每一个皇后 都判断一下 它的 行,列 ,左右对角线上 有没有皇后 深搜解决 ...
- PAT 甲级 1128 N Queens Puzzle
https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...
- PAT甲级:1152 Google Recruitment (20分)
PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...
- PAT A1128 N Queens Puzzle (20 分)——数学题
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...
- A1128. N Queens Puzzle
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...
- PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT 甲级 1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
- PAT 甲级 1011 World Cup Betting (20)(20 分)
1011 World Cup Betting (20)(20 分)提问 With the 2010 FIFA World Cup running, football fans the world ov ...
随机推荐
- 在Panel上绘图的实现
近期制作了FDS的一个建模工具,由于知识有限,做出的效果是2D的.昨天上课的时候看老师画一个长方体,突然想到,为什么不给普通的2D图形加画上几条直线,就能实现2D图形的3D视觉效果呢?于是回来马上做了 ...
- HDU6331Problem M. Walking Plan
传送门 分块floyd $f[i][j][k]$表示从i走k步到j的最短路 $g[i][j][k]$表示从i走k*100步到j的最短路 $h[i][j][k]$表示从i至少走k步到j的最短路 询问从x ...
- NX二次开发-UFUN求对象的最大边界框UF_MODL_ask_bounding_box
NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...
- Visual Studio Code 的简单试用体验
首先对Visual Studio Code做一个大概的介绍.首先明确一下,这个Visual Studio Code(以下简称 vscode)是一个带GUI的代码编辑器,也就是只能完成简单的代码编辑功能 ...
- Python对象继承set类型
Python对象继承set类型 class Feature(set): def __init__(self): set.__init__(self) # 这里演示将Feature类的加号重载成set. ...
- python之tkinter学习目录
前言 下面的目录结构,采用的学习视频资料是网易云课堂中[莫凡]老师的,在目录的最下面的地方给出了对应的链接! 学习是逐渐积累起来的,代码也是!下面的每一篇中的对应代码,都秉承着这样的一个理念:代码是成 ...
- Apache Solr 远程命令+XXE执行漏洞(CVE-2017-12629)
Apache Solr 最近有出了个漏洞预警,先复习一下之前的漏洞 命令执行 先创建一个listener,其中设置exe的值为我们想执行的命令,args的值是命令参数 POST /solr/demo/ ...
- 安装mongo
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_40101530/articl ...
- 深入理解 js为什么没有函数重载,如何实现函数重载?
我的新博客 http://www.suanliutudousi.com/2017/08/24/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3-js%E4%B8%BA%E4%B ...
- DRF的JWT用户认证
目录 DRF的JWT用户认证 JWT的认证规则 JWT的格式 JWT认证的流程 JWT模块的导入为 JWT的使用 DRF的JWT用户认证 从根本上来说,JWT是一种开放的标准(RFC 7519), 全 ...