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. 使用NFS时的一些问题

    当我把nfs服务端共享目录/usr/local/data/test删掉时,在nfs客户端却没办法把之前挂载在这上面的当前从机上的/usr/local/data/test删除,出现 bash: cd: ...

  2. python[练习题]:实现Base64编码

    要求自己实现算法,不用库. Base64简介: Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多 ...

  3. QTP 自动货测试桌面程序-笔记(添加控件仓库并关联到action)

    录制或编写脚本前先添加行为对应的控件仓库: 心得:多个测试action使用的同一个窗体的仓库单独放于一个仓库中,可以在多个action中添加关联使用 将同一个窗体的控件只放于一个仓库中,减少使用时的名 ...

  4. dw擴展jquery

    https://jingyan.baidu.com/article/90895e0fbbb65764ec6b0bd1.html

  5. 所活天数!java Date应用

    package cn.jiu.com; import java.text.ParseException; import java.text.SimpleDateFormat; import java. ...

  6. Eclipse:报错Failed to read artifact descriptor for org.springframework.boot:spring-boot-autoconfigure:jar:2.1.2.

    导入SVN下载的MAVEN项目时springboot报错: pom.xml文件报错 Failed to read artifact descriptor for org.springframework ...

  7. SELECT IDENT_CURRENT(tableName)和自增长列的纠结

    项目中要用到一个功能:主表主键为自增长,要求在插入主表数据前先获得主表要插入数据的主键ID值,用在插入子表时获取主键ID.在网上搜了一下,发现SELECT IDENT_CURRENT(TableNam ...

  8. C 语言-----字符串和输入输出函数

    在C语言中,没有字符串类型,那它是怎么表示字符串呢? 由于字符串是一系列单个字符的组合,所以它用char 类型的数组来表示字符串,在数组中,一个数组元素存放一个char类型字符. ]; name 变量 ...

  9. 在idea中设置记住git的用户名和密码

    在idea中设置记住git的用户名和密码 1.在项目根目录下执行以下git命令: git config --global credential.helper store 2.执行上述命令后,在idea ...

  10. CF558E-A Simple Task-线段树+计数排序

    计数排序的原理,只要知道了有几个数比i小,就可以知道i的位置 这道题只有26个字母,搞26颗线段树,然后区间更新 #include <cstdio> #include <cstrin ...