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. 矩阵连乘 LRJ白书 p141 栈 解析表达式

    #include<iostream> #include<cstdio> #include<cstring> #include<sstream> #inc ...

  2. [bzoj4025]二分图_LCT

    二分图 bzoj-4025 题目大意:给定一个n个节点的图,m条边,每条边有一个产生时间和一个删除时间,询问所有时间点是否是连通图. 注释:$1\le n\le 10^5$,$1\le m\le 2\ ...

  3. [bzoj3126][USACO2013]Photo_动态规划_单调队列

    Photo bzoj-3126 题目大意:给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问最多能有多少个点. 注释:$1\le n \le 2\cdot 10^5$,$1\le m\le1 ...

  4. assign retain 和copy的区别

    assign 对基础数据类型 (NSInteger,CGFloat)和C数据类型(int, float, double, char)等 等. 此标记说明设置器直接进⾏行赋值,这也是默认值.在使⽤用垃圾 ...

  5. Linux环境变量设置命令export(转)

    Linux export命令用于设置或显示环境变量. 在shell中执行程序时,shell会提供一组环境变量.export可新增,修改或删除环境变量,供后续执行的程序使用.export的效力仅及于该次 ...

  6. IDFTP连不上FTP服务器的解决方法

    使用IDFTP连不上FTP服务端. 原来是IDFTP有2种模式:主动模式和被动模式. IDFTP默认是使用主动模式,如果FTP服务端设置使用被动模式,使用IDFTP连不上FTP服务端的. 解决方法: ...

  7. CF #327 DIV2 D、E

    两题都不难. 对于D题,可以使用相对移动,把吹aircraft移动变成相反方向的待援点的移动.假设此时时间为t,把aircraft的速度设为Vmax,看待援点在飞船最大速度飞行t秒的范围内,注意风向变 ...

  8. FFmpeg基础库编程开发学习笔记——音频常见格式及字幕格式

    声明一下:这些关于ffmpeg的文章仅仅是用于记录我的学习历程和以便于以后查阅,文章中的一些文字可能是直接摘自于其它文章.书籍或者文献,学习ffmpeg相关知识是为了使用在Android上,我也才是刚 ...

  9. scikit-learn:通过Non-negative matrix factorization (NMF or NNMF)实现LSA(隐含语义分析)

    之前写过两篇文章.各自是 1)矩阵分解的综述:scikit-learn:2.5.矩阵因子分解问题 2)关于TruncatedSVD的简介:scikit-learn:通过TruncatedSVD实现LS ...

  10. 王立平--Failed to pull selection

    解决的方法:重新启动eclipse