1128. N Queens Puzzle (20) 判断是否是对角线

简介: 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.

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 <iostream>
#include <vector>
using namespace std;

//abs():取绝对值后取整   fabs():直接取绝对值
int main(){
    int k, n;
    cin >> k;
    for (int i = 0; i < k; i++) {
        cin >> n;
        vector<int> v(n);//直接开int数组 测试点3会快1ms  考虑到有好几台服务器 还是忽略这一点吧
        bool result = true;
        for (int j = 0; j < n; j++) {
            cin >> v[j];
            for (int l = 0; l < j; l++) {
                if (v[j] == v[l] || abs(v[j] - v[l]) == abs(j - l)) {
                    result = false;
                }
            }
        }
        cout << (result == true ? "YES" : "NO") << endl;
    }
    return 0;
}


目录
相关文章
|
7月前
|
机器学习/深度学习 人工智能 vr&ar
LHM:单图生成3D动画人!阿里开源建模核弹,高斯点云重构服装纹理
阿里巴巴通义实验室开源的LHM模型,能够从单张图像快速重建高质量可动画化的3D人体模型,支持实时渲染和姿态控制,适用于AR/VR、游戏开发等多种场景。
1509 0
LHM:单图生成3D动画人!阿里开源建模核弹,高斯点云重构服装纹理
|
语音技术 计算机视觉
|
10月前
|
负载均衡 算法 Linux
LVS+Keepalived:实现高效软负载均衡的利器
本文介绍了如何使用LVS(Linux Virtual Server)和Keepalived搭建高可用负载均衡集群。LVS通过不同调度算法将请求转发给后端服务器,而Keepalived基于VRRP协议实现服务高可用,避免IP单点故障。具体步骤包括环境准备、安装配置ipvsadm和Keepalived、启动服务及测试。文中还详细解释了配置文件中的关键参数,并提供了故障转移测试方法。最后,文章简要对比了软件、硬件和云负载均衡方案的特点,帮助读者选择合适的负载均衡策略。
1425 4
|
存储 API 调度
OpenStack核心组件Cinder
【8月更文挑战第4天】
602 9
|
机器学习/深度学习 数据采集 人工智能
一文看尽LLM对齐技术:RLHF、RLAIF、PPO、DPO……
【8月更文挑战第27天】本文全面回顾了近期大型语言模型(LLMs)领域内提升模型与人类价值观一致性的重要进展与挑战。尽管自监督学习及大规模预训练等技术推动了LLMs的快速发展,但如何避免生成不当内容仍是难题。文中系统地将现有研究分为奖励模型、反馈机制、强化学习策略及优化方法四大主题,并深入探讨各技术路径的创新点与局限性,如RLHF、RLAIF等方法。旨在为读者提供清晰的领域概览,促进未来研究发展。[论文链接](https://arxiv.org/pdf/2407.16216)
617 3
|
10月前
|
机器学习/深度学习 人工智能 自然语言处理
模型训练数据-MinerU一款Pdf转Markdown软件
MinerU是由上海人工智能实验室OpenDataLab团队开发的开源智能数据提取工具,专长于复杂PDF文档的高效解析与提取。它能够将含有图片、公式、表格等多模态内容的PDF文档转化为Markdown格式,同时支持从网页和电子书中提取内容,显著提升了AI语料准备的效率。MinerU具备高精度的PDF模型解析工具链,能自动识别乱码,保留文档结构,并将公式转换为LaTeX格式,广泛适用于学术、财务、法律等领域。
1290 4
|
存储 JSON Linux
portainer使用二进制文件安装
portainer使用二进制文件安装
|
存储 SQL 分布式计算
基于Hadoop豆瓣电影数据分析(综合实验)
基于Hadoop豆瓣电影数据分析(综合实验)
1852 1
基于Hadoop豆瓣电影数据分析(综合实验)
|
XML JSON Java
@PostMapping 必须加上@RequestBody吗
@PostMapping 必须加上@RequestBody吗
456 2
|
应用服务中间件
Error running ‘Tomcat 9.0.33‘: Can‘t find catalina.jar
Error running ‘Tomcat 9.0.33‘: Can‘t find catalina.jar
491 1