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. vue2.X的路由

    以 / 开头的嵌套路径会被当作根路径. <router-link> 在vue-router1.X中是以<a v-link=""></a>存在的 ...

  2. 【工具相关】Web-XAMPP的安装

    一,在查度中搜索XAMPP,会如下所示,下载软件 ,依次进行安装. 二,安装完成之后,会生成如下的图标. 三,打开上图的图标,如下图所示. 四,选择Apache Web Server---->S ...

  3. js 键盘码

    键盘各按键对应的数字 keycode 9 = Tab keycode 12 = Clear keycode 13 = Enter keycode 16 = Shift keycode 17 = Con ...

  4. .NetCore(四) 在Nginx部署

    ​本篇主要体验一下Nginx的使用,之前只接触过IIS. 一.Nginxa) ASP.NET Core内置了Kestrel服务器,但功能简单,主要用于SelfHost,正式运行还是要依赖IIS.Apa ...

  5. zookeeper.Net

    原文转至:http://www.cnblogs.com/shanyou/p/3221990.html 之前整理过一篇文章<zookeeper 分布式锁服务>,本文介绍的 Zookeeper ...

  6. LeetCode题解之Diameter of Binary Tree

    1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } ...

  7. .NET 控制Windows文件和目录访问权限研究(FileSystemAccessRule)

    前一段时间学习了.net 控制windows文件和目录权限的相关内容,期间做了一些总结.想把这方面的研究跟大家分享,一起学习.其中不免得有些用词不太标准的地方,希望大家留言指正,我加以修改. 首先,我 ...

  8. fedora 28 , firewalld 防火墙控制,firewall-cmd 管理防火墙规则

    今天,在使用fedora时,需要修改防火墙规则,一时间忘记了命令是什么,这里进行记录一下. 目前 fedora 28/ centos 7 使用 firewalld 作为防火墙软件:下面我就怎么简单管理 ...

  9. 安全之路 —— C/C++实现利用添加注册表项实现文件自启动

    简介 添加注册表项是实现文件自启动的经典方法之一,但因为操作注册表项是一个敏感操作,被报毒可能性较大,但即便如此,这个方法还是值得一学的,因为后期大部分编程都涉及到注册表操作. 最常使用到的注册表项有 ...

  10. 解决pycharm下代码报错的问题

    file-----settings------project:Pythonstudy------project structure-----选择要导入的文件并点击OK