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. php表单常用正则表达式

    <?php /** * @description: 正则表达式匹配 */ class Regex { /** * @手机号 */ public static function Phone($su ...

  2. redo allocation latch redo copy latch

    这两个latch 是干什么的一直有点迷糊,刚才上网查了一下,总结如下: redo allocation latch 在Log Buffer中分配内存空间时需要获取Redo allocation lat ...

  3. Mac下OpenCV开发

    1.         环境搭建 a)       安装Homebrew i.            下载地址:http://github.com/mxcl/homebrew/tarball/maste ...

  4. jQuery toast message 地址 使用

    jQuery toast message 地址 使用 https://github.com/akquinet/jquery-toastmessage-plugin/wiki

  5. hadoop常见操作命令

    1.查看指定文件夹下内容 hadoop dfs –ls [文件文件夹] eg: hadoop dfs –ls /user/wangkai.pt 2.打开某个已存在文件 hadoop dfs –cat ...

  6. JavaScript的代码库

    JavaScript的代码库 本文主要是汇集了一些JavaScript中一些经常使用代码.方便以后查找和复用. javascript框架: <script language="java ...

  7. JArray获取元素值

    MXS&Vincene  ─╄OvЁ  &0000003 ─╄OvЁ  MXS&Vincene MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好 ...

  8. vbs socket

    http://www.bathome.net/thread-423-1-1.html http://files.cnblogs.com/files/developer-ios/mswinsck.ocx ...

  9. luogu1006 传纸条

    题目大意 小渊坐在矩阵的左上角,坐标 (1,1 ),小轩坐在矩阵的右下角,坐标 (m,n) .从小渊传到小轩的纸条只可以向下或者向右传递,从小轩传给小渊的纸条只可以向上或者向左传递. 在活动进行中,小 ...

  10. B1257 [CQOI2007]余数之和 数学,分块

    这个题想明白之后很好做,但是不好想.我根本没想出来,上网看了一下才知道怎么做... 这个题其实得数是一个等差数列,然后一点点求和就行了. 上次NOIP就是没看出来规律,这次又是,下次先打表找规律!!! ...