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 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 K lines 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 <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】的更多相关文章

  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

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

  4. PAT甲级:1152 Google Recruitment (20分)

    PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...

  5. PAT A1128 N Queens Puzzle (20 分)——数学题

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

  6. A1128. N Queens Puzzle

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

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

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

  8. PAT 甲级 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

  9. 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 ...

随机推荐

  1. GitHub不能访问问题

    在C:\Windows\System32\drivers\etc文件夹下HOSTS里面最后添加: 192.30.253.112 github.com 192.30.253.113 github.com ...

  2. 安装percona-toolkit.rpm时候报错:perl(Time::HiRes) is needed by percona-toolkit-2.2.16-1.noarch

    1.安装percona-toolkit.rpm时候报错: warning: percona-toolkit.rpm: Header V4 DSA/SHA1 Signature, key ID cd2e ...

  3. delphi xe10 蓝牙

    //蓝牙 System.Bluetooth //单元中主要包含以下几个类 TBluetoothManager.TBluetoothDeviceList.TBluetoothAdapter.TBluet ...

  4. 2019/11/8 CSP模拟

    T1 药品实验 内网#4803 由概率定义,有\[a + b + c = 0\] 变形得到\[1 - b = a + c\] 根据题意有\[p_i = a p _{i - 1} + b p_i + c ...

  5. 牛客多校第九场 B Quadratic equation 模平方根

    题意: 已知 $x+y$ $mod$ $q = b$ $x*y$ $mod$ $q = c$ 已知b和c,求x和y 题解: 容易想到$b^2-4c=x^2-2xy+y^2=(x-y)^2$ 那么开个根 ...

  6. natapp自动获取免费的动态端口域名

    前段时间,因为客户有个项目要求跨局域网进行远程控制桌面,想知道能不能实现.于是查询了许多资料,了解到需要有公网服务器作为中介才能够实现,但是公司又没有公网服务器,于是只有利用花生壳.natapp服务器 ...

  7. VS2010-MFC(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)

    转自:http://www.jizhuomi.com/software/255.html 上一节讲了为Ribbon Bar添加控件的方法.本节教程将继续完善前面的实例,讲解一些稍复杂的控件的添加方法, ...

  8. ionic-CSS:ionic checkbox(复选框)

    ylbtech-ionic-CSS:ionic checkbox(复选框) 1.返回顶部 1. ionic checkbox(复选框) ionic 里面的 Checkbox 和普通的 Checkbox ...

  9. sift-高斯金字塔

    一.高斯金字塔 金字塔的层是由降采样得到的,而每一层又有多张图像,其他的图像是由初始的一张经过高斯模糊的得到的. 注意的是:高斯金字塔有层,而金字塔的每一层有一组图像,这一组图像也形成了层.注意两个层 ...

  10. ps制作浮雕和投影效果

    1用文字横排工具写个hope,按住ctrl+t可以调试出文字工具,上面直接用500点来改变文字的大小.2 用矩形选框工具直接可以切割图片的大小,然后双击一个图层,添加样式为浮雕....然后合并图层3 ...