PAT 甲级 1128 N Queens Puzzle
https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8chessboard 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 <bits/stdc++.h>
using namespace std; int N;
int a[1111], line[1111], row[1111]; int main() {
int K;
scanf("%d", &K);
while(K --) {
scanf("%d", &N); for(int i = 1; i <= N; i ++) {
scanf("%d", &a[i]);
} memset(line, 0, sizeof(line));
memset(row, 0, sizeof(row)); for(int i = 1; i <= N; i ++) {
line[a[i]] ++;
row[i] ++;
} bool flag = true;
for(int i = 1; i <= N; i ++) {
if(line[i] != 1 || row[i] != 1)
flag = false;
} for(int i = 2; i <= N; i ++) {
for(int j = 1; j < i; j ++) {
if(abs(a[i] - a[j]) == abs(i - j))
flag = false;
}
} if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
PAT 甲级 1128 N Queens Puzzle的更多相关文章
- PAT甲级 1128. N Queens Puzzle (20)
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 甲级 1128. N Queens Puzzle (20) 【STL】
题目链接 https://www.patest.cn/contests/pat-a-practise/1128 思路 可以 对每一个皇后 都判断一下 它的 行,列 ,左右对角线上 有没有皇后 深搜解决 ...
- PAT甲级——A1128 N Queens Puzzle【20】
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
- PAT 1128 N Queens Puzzle
1128 N Queens Puzzle (20 分) The "eight queens puzzle" is the problem of placing eight ch ...
- PAT 1128 N Queens Puzzle[对角线判断]
1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...
- 1128 N Queens Puzzle (20 分)
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
- PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 1128 N Queens Puzzle
题意:给定一串序列,判断其是否是合法的N皇后方案. 思路:本题是阅读理解题,不是真的N皇后问题.N皇后问题的合法序列要求任意两个皇后不在同一行.同一列,以及不在对角线.本题已经明确不会在同一列,故只需 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- day 21继承
1.了解Python2和python3类的区别: python2.3之前使用的是经典类, 在2.3版本之后组,使用的是新式类 MRO: method resolution order 方法的查找 ...
- 第三篇 : vi编辑器配置与基本操作
目录 一.vi编辑器的配置 二.一般模式下的常用操作 一.vi编辑器的配置 配置文件位置 #配置文件virc(vi);vimrc(vim) cd /etc/vim //配置文件有在这目录的,也有可能是 ...
- 『Python基础-11』集合 (set)
# 『Python基础-11』集合 (set) 目录: 集合的基本知识 集合的创建 访问集合里的值 向集合set增加元素 移除集合中的元素 集合set的运算 1. 集合的基本知识 集合(set)是一个 ...
- HTTP学习之URL与资源
URL是因特网资源的标准化名称,该字符串指向一条电子信息片段,定义服务端应用程序在什么位置以及客户端要如何与其交互 一条完整的URL由多个片段组成. 通用URL组件 方案 以哪种协议访问服务器 用户 ...
- ruby配置镜像源
1.打开电脑的cmd窗口,输入如下命令即可查看gem镜像: gem sources l 或是直接使用 gem sources 查询结果如下: C:\Users\Administrator>gem ...
- U盘kali系统安装
正言: 起初先百度了一下U盘安装Kali的资料,有很多版本和方法,当然还是以百度经验为例开始操作https://jingyan.baidu.com/article/cdddd41ca1027e53 ...
- UART学习之路(二)基本时序介绍
这次我们来介绍一下UART的基本时序,了解一下底层信号怎么传送的.方便以后使用Verilog HDL实现收发逻辑. 9600bit/s 的意思是每秒发送9600bit,因此可以理解为将1s分解为960 ...
- 小程序开发-9-Behavior行为与加入缓存系统优化流行页面
Behavior行为与加入缓存系统优化流行页面 navi组件与移动端触碰区域探讨 触碰区域优化 设计师切图切大点,多余部分变成透明色 前端将可触碰区域变大 解决向左箭头变灰,向右变灰 禁用事件的技巧 ...
- Java 高级应用编程 第一章 工具类
一.Java API Java API简介 1.API (Application Programming Interface) 应用程序接口 2.Java中的API,就是JDK提供的各种功能的Java ...
- 北京Uber优步司机奖励政策(2月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

