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. no_merge hint

    This is tested in 10gR2. SQL> select * from v$version; BANNER ----------------------------------- ...

  2. PKU 2774 Long Long Message (后缀数组练习模板题)

    题意:给你两个字符串.求最长公共字串的长度. by:罗穗骞模板 #include <iostream> #include <stdio.h> #include <stri ...

  3. 工作easy,赚钱非常难

    李宗盛有首歌的歌词里写到:「工作是easy的,赚钱是困难的」. 乍一听感觉有点矛盾,工作的一个重要结果不就是赚钱么,为什么工作easy赚钱却难?但细致一想就恍然当中想表达的意思了. 工作的本质是出售劳 ...

  4. Eclipse集成Resinserver

        因为Resin在Eclipse下的表现丝毫不亚于Tomcat,小编决定带领众小弟一起学习使用Resin.虽然小编身边也没有什么大牛在使用Resin,但看到Resin的广告已经吹到天边了.所以还 ...

  5. Kafka编程实例

    编程 Producer是一个应用程序.它创建消息并发送它们到Kafka broker中.这些producer在本质上是不同.比方.前端应用程序.后端服务.代理服务.适配器对于潜在的系统,Hadoop对 ...

  6. USACO money packageDP

    裸0/1背包,就是从各种币种里面拿来凑足N元,求最多有多种方案.用dp[i][j]表示选前i个币种凑成j的方案数量 状态转移方程: dp[i][j] = dp[i- 1][j]    j < c ...

  7. hdu 2602(经典01背包)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. Spark之MLlib

    目录 Part VI. Advanced Analytics and Machine Learning Advanced Analytics and Machine Learning Overview ...

  9. SpringMVC之DispatcherServlet详解

    SpringMVC是一种基于请求启动的WEB框架,并且使用了前端控制器的设计模式,所有满足[WEB-INF/web.xml]文件中的[url-pattern]的匹配条件的请求,这些满足的请求都会交给这 ...

  10. go之数组

    一.数组概念 go语言提供了数组类型的数据结构 数组是具有 [唯一类型] 的一组 [固定长度] 的数据项序列,这种类型可以是任意类型 二.数组声明 var variable_name [SIZE]va ...