The "eight queens puzzle" is the problem of placing eight chess queens on an 8×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 (Q1, Q2, ..., QN), where Qi 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 < K <= 200). Then K lines follow, each gives a configuration in the format "N Q1 Q2 ... QN", where 4 <= N <= 1000 and it is guaranteed that 1 <= Qi <= N for all i=1, ..., N. 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<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int num[], N, K, hashTB[];
int main(){
scanf("%d", &K);
for(int i = ; i < K; i++){
scanf("%d", &N);
fill(hashTB, hashTB + , );
for(int j = ; j <= N; j++){
scanf("%d", &num[j]);
hashTB[num[j]]++;
}
int tag = ;
for(int j = ; j <= N; j++){
if(hashTB[j] != ){
tag = ;
break;
}
int m = j - , n = num[j] - ;
while(m >= && m <= N && j >= && j <= N){
if(num[m] == n){
tag = ;
break;
}
m--; n--;
}
m = j + ; n = num[j] + ;
while(m >= && m <= N && j >= && j <= N){
if(num[m] == n){
tag = ;
break;
}
m++; n++;
}
}
if(tag == )
printf("NO\n");
else printf("YES\n");
}
cin >> N;
return ;
}

总结:

1、由于已经保证了不在同一列,所以只需要检查行和斜线即可。

2、检查a、b两点间的斜线,可用abs(Xa - Xb) == abs(Ya - Yb)。

A1128. N Queens Puzzle的更多相关文章

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

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

  2. PAT甲级——A1128 N Queens Puzzle【20】

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

  3. PAT_A1128#N Queens Puzzle

    Source: PAT A1128 N Queens Puzzle (20 分) Description: The "eight queens puzzle" is the pro ...

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

  5. Pat1128:N Queens Puzzle

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

  6. PAT 1128 N Queens Puzzle

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

  7. PAT甲级 1128. N Queens Puzzle (20)

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

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

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

  9. PAT 甲级 1128 N Queens Puzzle

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

随机推荐

  1. 生成统计数据并导出Excel

    需求:看如下表格的统计需求 生产调度中心部门需要从IT技术部门得到这些统计数据 步骤: (1)获取所有的子公司列表 (2)遍历所有的子公司,获取每个子公司的库存信息 (3)遍历所有的库存信息,并对库存 ...

  2. jquery的show()和hide()方法

    显示和隐藏元素的方法 相当于CSS中的display属性 show()方法,相当于display:block hide()方法,相当于display:none

  3. sqlmap-学习1 配置环境

    sqlmap是一款非常强大的开源sql自动化注入工具,可以用来检测和利用sql注入漏洞.它由python语言开发而成,因此运行需要安装python环境 1 安装 python (https://www ...

  4. DBExpress动态连接SQL-Server

    procedure TForm1.Button1Click(Sender: TObject);var  theCNN : TSQLConnection;//定义连接,要引用 DB, SqlExprbe ...

  5. 虚拟机linux系统明明已经安装了ubuntu,但是每次重新进入就又是选择安装界面

    本文转载:https://blog.csdn.net/weixin_41522164/article/details/82814375

  6. SpringBoot之处理JSON数据举例

    SpringBoot使用@RequestBody注解会自动将请求body中的json数据绑定到参数上.使用@ResponseBody注解,在返回参数时自动将对象转换为JSON格式返回. 举例代码: c ...

  7. js笔记2

    原型:prototype 和 __proto__ prototype 给他即将生成的对象继承下去的属性 prototype: 显式原型,每个function下都有prototype属性,该属性是一个对 ...

  8. HDU 5025 Saving Tang Monk

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  9. codeforces498C

    Array and Operations CodeForces - 498C You have written on a piece of paper an array of n positive i ...

  10. ubuntu系统安装mysql(deb-bundle包)

    由于某些原因,又要在ubuntu系统中安装mysql了,之前曾经安装过好多次.都没记下来 以前一直动用源码包来安装,基于两个原因:1.一直用Python写代码.2.想使用文件来安装,而不是通过api ...