Source:

PAT A1128 N Queens Puzzle (20 分)

Description:

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

Keys:

Attention:

  • 给出的N个皇后可能在同一行

Code:

 /*
Data: 2019-05-25 10:24:05
Problem: PAT_A1128#N Queens Puzzle
AC: 18:04 题目大意:
判断给定的序列是否为N皇后问题的解
输入:
第一行给出:测试数K<=200;
接下来K行,问题规模N<=1e3,N个数
输出:
Yes or No
*/ #include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e3+; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,m;
scanf("%d", &m);
while(m--)
{
scanf("%d", &n);
int q[M],ans=;
for(int i=; i<=n; i++)
scanf("%d", &q[i]);
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
if(abs(j-i)==abs(q[j]-q[i]) || q[j]==q[i]){
ans=;break;
}
if(ans==)
break;
}
if(ans) printf("YES\n");
else printf("NO\n");
} return ;
}

PAT_A1128#N Queens Puzzle的更多相关文章

  1. Poj 3239 Solution to the n Queens Puzzle

    1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...

  2. Pat1128:N Queens Puzzle

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

  3. PAT 1128 N Queens Puzzle

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

  4. A1128. N Queens Puzzle

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

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

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

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

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

  8. PAT 甲级 1128 N Queens Puzzle

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

  9. 1128 N Queens Puzzle (20 分)

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

随机推荐

  1. [bzoj3037/2068]创世纪[Poi2004]SZP_树形dp_并查集_基环树

    创世纪 SZP bzoj-3037/2068 Poi-2004 题目大意:给你n个物品,每个物品可以且仅可以控制一个物品.问:选取一些物品,使得对于任意的一个被选取的物品来讲,都存在一个没有被选取的物 ...

  2. ZooKeeper可视化Web管理工具收集(待实践)

    原来ZooKeeper是有Web管理后台的.但是仅限于操作ZooKeeper的数据,如果要监控性能,估计要借助Nagios去配合. 这些工具应该ZK UI最好用,下面是收集的一些工具安装教程: htt ...

  3. javaweb项目中获取项目名称

    request.getServletContext().getContextPath() 增加项目名称是test.那么上面的结果就是/test

  4. MySQL desc作用

    MySQL中默认排序是acs(可省略):从小到大 desc :从大到小,也叫倒序排列.

  5. 查看scn headroom变化趋势的几种方法

    查看scn headroom变化趋势的几种方法 scn headroom问题,本文不做解释. 本文为自己的总结,脚本来自于oracle sr技术project师. 转载请注明出处http://blog ...

  6. Java中接口和抽象类的比較

    Java中接口和抽象类的比較-2013年5月写的读书笔记摘要 1. 概述 接口(Interface)和抽象类(abstract class)是 Java 语言中支持抽象类的两种机制,是Java程序设计 ...

  7. oc61--block

    // // main.m // Block基本使用:一种数据类型,应用在动画,多线程,集合遍历,网络请求回调. // 用来保存一段代码,在恰当的时候拿出来调用.功能类似于函数.函数不能嵌套定义,blo ...

  8. 通过Ajax和SpringBoot交互的示例

    转自:https://blog.csdn.net/oppo5630/article/details/52093898/

  9. E20170902-hm

    devise v. 设计; 想出; 发明; 策划;   n. 遗赠; 遗赠的财产; 遗赠的条款; device n. 设备  

  10. Django day14(一) cookie

    一: Cookie 1.  Cookie是什么?存储在客户端浏览器上的键值对 2. 原理: 是服务器产生,发给客户端浏览器,浏览器保存起来,下次发请求,会携带这个键值对到服务器 4. Cookie的覆 ...