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, 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 (Q​1​​,Q​2​​,⋯,Q​N​​), where Q​i​​ 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<K≤200). Then K lines follow, each gives a configuration in the format "N Q​1​​ Q​2​​ ... Q​N​​", where 4≤N≤1000 and it is guaranteed that 1≤Q​i​​≤N for all i=1,⋯,N. 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 <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int k,n;
int a[];
int main(){
scanf("%d",&k);
for(int i=;i<k;i++){
fill(a,a+,);
scanf("%d",&n);
int flag=;
for(int j=;j<=n;j++){
int tmp;
scanf("%d",&tmp);
if(flag==){
a[j]=tmp;
for(int q=;q<j;q++){
if(abs(j-q)==abs(a[j]-a[q]) || a[j]==a[q]){
flag=;
break;
}
}}
else continue;
}
printf("%s\n",flag==?"NO":"YES");
}
}

注意点:测试点1是有两个在同一行,所以这题是不仅判断对角线,还有同行,但肯定不会同列。

PAT A1128 N Queens Puzzle (20 分)——数学题的更多相关文章

  1. PAT 1128 N Queens Puzzle

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

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

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

  3. PAT乙级:1088 三人行 (20分)

    PAT乙级:1088 三人行 (20分) 题干 子曰:"三人行,必有我师焉.择其善者而从之,其不善者而改之." 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整 ...

  4. PAT乙级:1064 朋友数 (20分)

    PAT乙级:1064 朋友数 (20分) 题干 如果两个整数各位数字的和是一样的,则被称为是"朋友数",而那个公共的和就是它们的"朋友证号".例如 123 和 ...

  5. PAT甲级——A1128 N Queens Puzzle【20】

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

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

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

  7. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  8. A1128. N Queens Puzzle

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

  9. [PAT] 1140 Look-and-say Sequence(20 分)

    1140 Look-and-say Sequence(20 分)Look-and-say sequence is a sequence of integers as the following: D, ...

随机推荐

  1. Excel破解工作表保护

    宏运行 Public Sub Password_cracking() Const DBLSPACE As String = vbNewLine & vbNewLine Const AUTHOR ...

  2. canvas实现验证码

    在通常的登录界面我们都可以看到验证码,验证码的作用是检测是不是人在操作,防止机器等非人操作,防止数据库被轻而易举的攻破. 验证码一般用PHP和java等后端语言编写. 但是在前端,用canva或者SV ...

  3. 2018-02-17 中文代码示例[译]Scala中创建隐式函数

    前言: 学习Scala时, 顺便翻译一下自己有兴趣的文章. 代码中所有命名都中文化了(不是翻译). 比如原文用的是甜甜圈的例子. 原文: Scala Tutorial - Learn How To C ...

  4. python之操作系统介绍,进程的创建

    操作系统(英语:operating system,缩写作 OS)是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内核与基石.操作系统需要处理如管理与配置内存.决定系统资源供需的优先次序.控 ...

  5. ArcGIS地图文档优化 mxdPerfstat工具使用体验

    经常有客户会咨询到如何提高地图的显示性能.为何ArcMap刷新地图那么缓慢.为何地图服务响应要等待10多秒? 诸如这些问题,虽然它们的表象都是相似的,但是往往在分析排查问题的时候,我们发现背后的原因是 ...

  6. 测试思想-测试设计 史上最详细测试用例设计实践总结 Part2

    史上最详细测试用例设计实践总结 by:授客 QQ:1033553122 -------------------------接 Part1-------------------------- 方法:这里 ...

  7. Linux笔记(二): WIN 10 Ubuntu 双系统

    (一)  说明 记录一次ubuntu安装过程及遇到的问题. 环境:WIN 10 单硬盘 (二)  ubuntu ISO文件下载 ubuntu 18.04 https://www.ubuntu.com/ ...

  8. npm与yarn常用命令对比

    最近在用yarn,但是命令老是记不住,在此记录,方便日后翻看 图片截取自:http://yuanhehe.cn/2017/06/11/npm-%E4%B8%8E-Yarn-%E5%B8%B8%E7%9 ...

  9. Centos7下gogs数据配置迁移

    工作需要把测试版的gogs迁移到正式版gogs,顺便记下笔记防止以后需要忘记了 环境: centos7测试版gogs:192.168.1.63 centos7正式版gogs:192.168.0.100 ...

  10. MyEclipse10或者eclipse中配置开发Python的Pydev插件安装教程

    注意使用LiClipse的用户 PyDev已经预装在LiClipse中,所以可以跳过这一步(请注意,如果使用LiClipse,PyDev不能单独安装或更新,因为它必须始终作为一个整体更新). 必需品 ...