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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- python逻辑判断 () not and or
python逻辑判断 () not and or 优先级关系:()>not>and>or 运算符示意 not –表示取反运算. and –表示取与运算. or –表示取或运算. or ...
- Leecode刷题之旅-C语言/python-349两个数组的交集
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
- 【转】I2C总线相关知识
1. I2C access 1.1. I2C introduction I2C(Inter-Integrated Circuit)总线是由NXP恩智浦半导体公司在80年代开发的两线式串行总线,用来进行 ...
- python的第一个程序“Hello,World”,传闻要想学好新语言....
传闻要想学好新语言,第一个程序必须是“Hello,World”...O(∩_∩)O哈哈~ 下面附上代码: # -*- coding:utf-8 -*- print("Hello,World& ...
- 《C++ Primer》第II部分:C++标准库
<C++ Primer>第II部分:C++标准库 前言 把<C++ Primer>读薄系列笔记.本篇为第II部分C++标准库,包含全书第8-12章重难点: IO库 顺序容器 范 ...
- Java:String、StringBuffer、StringBuilder
一.String 1. String类是final类,意味着String类不能被继承,它的成员方法都默认为final方法.在早期的JVM版本中,被final修饰的方法会转为内嵌调用来提升执行效率.从J ...
- zabbix经常报警elasticsearch节点TCP连接数过高问题
单服务器最大tcp连接数及调优汇总 单机最大tcp连接数 网络编程 在tcp应用中,server事先在某个固定端口监听,client主动发起连接,经过三路握手后建立tcp连接.那么对单机,其最大并发t ...
- ubuntu网卡
查看网卡类型 http://blog.csdn.net/eddy_liu/article/details/6578819 qii@ubuntu:~$ lspci | grep -i net 03:0 ...
- springBoot 自定义redisTemplate
package com.atirm.mybatismutiplesource.config.RedisConfig; import com.atirm.mybatismutiplesource.ent ...
- redis 学习笔记二
redis启动: 直接 redis-server.exe 启动服务,是按照redis默认配置启动的,如果想按照自己的配置文件启动,要加上 redis-server.exe redis.windows ...