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;
}


目录
相关文章
|
负载均衡 算法 Linux
LVS+Keepalived:实现高效软负载均衡的利器
本文介绍了如何使用LVS(Linux Virtual Server)和Keepalived搭建高可用负载均衡集群。LVS通过不同调度算法将请求转发给后端服务器,而Keepalived基于VRRP协议实现服务高可用,避免IP单点故障。具体步骤包括环境准备、安装配置ipvsadm和Keepalived、启动服务及测试。文中还详细解释了配置文件中的关键参数,并提供了故障转移测试方法。最后,文章简要对比了软件、硬件和云负载均衡方案的特点,帮助读者选择合适的负载均衡策略。
1893 4
|
存储 API 调度
OpenStack核心组件Cinder
【8月更文挑战第4天】
793 9
|
机器学习/深度学习 数据采集 人工智能
一文看尽LLM对齐技术:RLHF、RLAIF、PPO、DPO……
【8月更文挑战第27天】本文全面回顾了近期大型语言模型(LLMs)领域内提升模型与人类价值观一致性的重要进展与挑战。尽管自监督学习及大规模预训练等技术推动了LLMs的快速发展,但如何避免生成不当内容仍是难题。文中系统地将现有研究分为奖励模型、反馈机制、强化学习策略及优化方法四大主题,并深入探讨各技术路径的创新点与局限性,如RLHF、RLAIF等方法。旨在为读者提供清晰的领域概览,促进未来研究发展。[论文链接](https://arxiv.org/pdf/2407.16216)
795 3
|
存储 SQL 分布式计算
基于Hadoop豆瓣电影数据分析(综合实验)
基于Hadoop豆瓣电影数据分析(综合实验)
1970 1
基于Hadoop豆瓣电影数据分析(综合实验)
|
存储 JSON Linux
portainer使用二进制文件安装
portainer使用二进制文件安装
|
XML JSON Java
@PostMapping 必须加上@RequestBody吗
@PostMapping 必须加上@RequestBody吗
668 2
|
缓存 关系型数据库 MySQL
业务中的字典表的MySQL实现方案
业务中的字典表的MySQL实现方案
699 0
业务中的字典表的MySQL实现方案
|
Java
SpringBoot 获取当前登录用户IP
控制器方法: @RequestMapping(value = "/getIp", method = RequestMethod.POST) @ResponseBody public String getIp(HttpServletRequest request) { return IpUtil.
9123 0
|
应用服务中间件
Error running ‘Tomcat 9.0.33‘: Can‘t find catalina.jar
Error running ‘Tomcat 9.0.33‘: Can‘t find catalina.jar
582 1
|
机器学习/深度学习 自然语言处理 PyTorch
机器学习-ROC曲线:技术解析与实战应用
机器学习-ROC曲线:技术解析与实战应用
845 0

热门文章

最新文章