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 Nqueens 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 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). Then Klines follow, each gives a configuration in the format "N Q​1​​ Q​2​​ ... Q​N​​", 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<cmath>
#include<vector>
using namespace std; int main(){
int n,k;
cin >> k;
for(int i = ; i < k; i++){
cin >> n;
vector<int> v(n+);
bool flag = true;
for(int j = ; j <= n; j++){
cin >> v[j];
for(int t = ; t < j; t++){
if(v[t] == v[j] || abs(j-t) == abs(v[j] - v[t])){
flag = false;
break;
}
}
}
cout << (flag == true ? "YES\n":"NO\n");
}
return ;
}

1128 N Queens Puzzle (20 分)的更多相关文章

  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甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题

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

  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. PAT 甲级 1128 N Queens Puzzle

    https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...

  7. 1128 N Queens Puzzle

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

  8. Pat1128:N Queens Puzzle

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

  9. PAT_A1128#N Queens Puzzle

    Source: PAT A1128 N Queens Puzzle (20 分) Description: The "eight queens puzzle" is the pro ...

随机推荐

  1. linux下配置eclipse环境

    注明:本文为博主原创文章,转载请注明出处 前期准备 (此文使用的是非安装版jdk1.8,你也可以下载版本更低的,而且建议使用1.6版本,66大顺嘛,嘻嘻) 1.点击下载jdk 2.点击下载eclips ...

  2. Luogu 4844 LJJ爱数数

    LOJ 6482 设$d = gcd(a, b)$,$xd = a$,$yd = b$,因为$\frac{1}{a} + \frac{1}{b} = \frac{a + b}{ab} = \frac{ ...

  3. 在Asp.Net中使用amChart统计图

    怎么在自己的ASP.NET页面插入可动态更新的数据统计图呢?网上的资源倒是不少(Fusioncharts.amCharts……),在这些资源中有一个比较好用:amChart,这个工具很炫,还能与用户交 ...

  4. CLR 显示实现事件 EventSet内部管理一个字典

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  5. linux学习1----初涉linux

    linux因其稳定高效的特点,受到很多开发者的青睐,因此将其作为服务器的操作系统. 作为一名开发者,程序员,掌握了一定的linux知识和技巧,程序的开发部署和运行也有不小的帮助. linux由于其开源 ...

  6. New for ASP.NET Web Pages: Conditional attributes

    from:http://www.mikepope.com/blog/AddComment.aspx?blogid=2353 March 01, 2012 The beta release of ASP ...

  7. HttpUploader7-授权码配置

    1.1. 七牛云存储 配置方式: 1.配置授权码   2.配置云存储   3.配置空间名称   4.配置上传地址   1.2. 阿里云存储 配置方式: 1.填写授权码   2.配置云存储为阿里云   ...

  8. memset函数使用

    函数原型 void *memset(void *s,int c,size_t n): 功能 将已开辟内存空间 s 的首 n 个字节的值设为值 c. 头文件  #include<memory.h& ...

  9. 转Delphi中XLSReadWrite控件的使用(1)---简介

    XLSReadWrite控件简介: 一个你需要的,能在Delphi和.NET下访问Excel文件的完美解决方案. 一个经典的读写Excel的控件,对于使用Excel 开发很有帮助 官方网站: http ...

  10. linux 虚拟机 安装 php-7.0.2

    1.安装依赖包 yum -y install libxml2 libxml2-devel openssl openssl-devel libjpeg libjpeg-devel libpng libp ...