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. mybatis两种开发方式

    本文首先讲解从JDBC到mybatis的演变过程,然后是使用mybatis进行开发的两种方式. 一 JDBC的使用及其优化 1.使用JDBC进行数据库操作 加载JDBC驱动: 建立并获取数据库连接: ...

  2. Docker操作删除所有容器镜像

    借鉴博客:https://www.cnblogs.com/yanyouqiang/p/8301856.html https://blog.csdn.net/wy_97/article/details/ ...

  3. linux audit审计(7)--读懂audit日志

    让我们先来构造一条audit日志.在home目录下新建一个目录,然后配置一条audit规则,对这个目录的wrax,都记录审计日志: auditctl -w /home/audit_test -p wr ...

  4. 莫烦theano学习自修第三天【共享变量】

    1. 代码实现 #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np import theano.tensor as T i ...

  5. php2

    session   //将用户的会话数据存储在服务端,通过 session_start()开启session,通过$_SESSION读写session session_start(); //开启ses ...

  6. MySQL的FIND_IN_SET()函数

    今天在做项目时,看到了一个从没见过的MySQL函数——FIND_IN_SET(),顿时就产生了浓郁的兴趣,然后就搜了搜,翻了翻. 语法:FIND_IN_SET(str,strlist) 定义: 1. ...

  7. JUC虚假唤醒(六)

    为什么条件锁会产生虚假唤醒现象(spurious wakeup)? ​ 在不同的语言,甚至不同的操作系统上,条件锁都会产生虚假唤醒现象.所有语言的条件锁库都推荐用户把wait()放进循环里: whil ...

  8. caffe网络中屏蔽某一层的输出Silence层

    屏蔽label输出 layer { name: "silence0" type: "Silence" bottom: "label" pha ...

  9. mobile deeplearning

    框架: 腾讯ncnn https://github.com/Tencent/ncnn 百度mobile-deep-learning https://github.com/baidu/mobile-de ...

  10. 只要访问url地址 那么容器就会根据地址进行对象的创建

    只要访问url地址 那么容器就会根据地址进行对象的创建