https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360

The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8chessboard 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
时间复杂度:$O(1/2 * n^2)$ 
代码:

#include <bits/stdc++.h>
using namespace std; int N;
int a[1111], line[1111], row[1111]; int main() {
int K;
scanf("%d", &K);
while(K --) {
scanf("%d", &N); for(int i = 1; i <= N; i ++) {
scanf("%d", &a[i]);
} memset(line, 0, sizeof(line));
memset(row, 0, sizeof(row)); for(int i = 1; i <= N; i ++) {
line[a[i]] ++;
row[i] ++;
} bool flag = true;
for(int i = 1; i <= N; i ++) {
if(line[i] != 1 || row[i] != 1)
flag = false;
} for(int i = 2; i <= N; i ++) {
for(int j = 1; j < i; j ++) {
if(abs(a[i] - a[j]) == abs(i - j))
flag = false;
}
} if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

  

PAT 甲级 1128 N Queens Puzzle的更多相关文章

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

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

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

    题目链接 https://www.patest.cn/contests/pat-a-practise/1128 思路 可以 对每一个皇后 都判断一下 它的 行,列 ,左右对角线上 有没有皇后 深搜解决 ...

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

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

  4. PAT 1128 N Queens Puzzle

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

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

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

  6. 1128 N Queens Puzzle (20 分)

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

  7. PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. 1128 N Queens Puzzle

    题意:给定一串序列,判断其是否是合法的N皇后方案. 思路:本题是阅读理解题,不是真的N皇后问题.N皇后问题的合法序列要求任意两个皇后不在同一行.同一列,以及不在对角线.本题已经明确不会在同一列,故只需 ...

  9. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. php ecshop 二级域名切换跳转时session不同步,解决session无法共享同步导致无法登陆或者无法退出的问题

    echshop基础上做了单点登录的 一级域名与二级域名 退出时 清空session 都是一级域名的session 因为二级域名的session是设置在二级域名上的 echshop基础上没有做单点登录的 ...

  2. 第7章 YARN HA配置

    目录 7.1 yarn-site.xm文件配置 7.2 测试YARN自动故障转移 ResourceManager (RM)负责跟踪集群中的资源,以及调度应用程序(例如,MapReduce作业).在Ha ...

  3. Hive命令行及参数配置

    1 . Hive  命令行 输入$HIVE_HOME/bin/hive –H 或者 –help 可以显示帮助选项: 说明: 1. -i 初始化 HQL 文件. 2. -e 从命令行执行指定的 HQL ...

  4. Makefile中的$(MAKE)

    今天看uboot2018顶层的Makefile中发现文件中export一个MAKE变量,export是为了向底层的Makefile传递这些变量参数,但是找了半天没有找到这个MAKE变量在哪定义的. 决 ...

  5. Qt——事件

    1.常见事件 [1]鼠标事件 (1)坐标 x(),y(), 相对windows globalX() globalY() (2)获得点击 button() [2]键盘事件 [3]定时器事件 timerI ...

  6. Grep/find查找文件

    1. 查找secret 函数所在的文件位置grep -rn secret * grep -rn "secret" * 2. find 查找当前目录下,比while2 时间新并且名字 ...

  7. SpringBoot-03:SpringBoot+Idea热部署

      ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 所谓热部署,就是在项目启动中,修改class类中做的修改操作,无需重新启动项目,就可以变更,在网页展示中有 ...

  8. Ajax中post请求和get请求的区别

    首先提出两点Post比Get大的不同地方 1.post请求浏览器每次不会缓存,每次都会重新请求,而get请求不要缓存的时候,需要手动设置 写上xhr.setRequestHeader("If ...

  9. logstash-input-jdbc and logstash-ouput-jdbc

    要求通过logstash从oracle中获取数据,然后相应的直接传入mysql中去. 基本测试成功的配置文件如下: input { stdin { } jdbc { jdbc_connection_s ...

  10. linux部署MantisBT(二)部署php

    二.部署php 1.下载php安装包 http://php.net/downloads.php 2.安装php yum install libxml2 yum install libxml2-deve ...