PAT甲级——A1128 N Queens Puzzle【20】
The "eight queens puzzle" is the problem of placing eight chess queens on an 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 (, 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). Then K lines follow, each gives a configuration in the format "N Q1 Q2 ... QN", where 4 and it is guaranteed that 1 for all ,. 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;
int queen[];
int main()
{
int k, n, a;
cin >> k;
while (k--)
{
fill(queen, queen + , );
cin >> n;
bool res = true;
for (int i = ; i <= n; ++i)
{
cin >> queen[i];//新一列存入queen
for (int t = ; t < i; ++t)//判断前i-1列的queen是不是在同一行
{
if (queen[i] == queen[t] || abs(queen[i] - queen[t]) == abs(i - t))//是否存在相同行,和第t列的斜线位置
{
res = false;
break;
}
}
}
cout << (res == true ? "YES" : "NO") << endl;
}
return ;
}
PAT甲级——A1128 N Queens Puzzle【20】的更多相关文章
- 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 甲级 1128 N Queens Puzzle
https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...
- PAT甲级:1152 Google Recruitment (20分)
PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...
- PAT A1128 N Queens Puzzle (20 分)——数学题
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...
- A1128. N Queens Puzzle
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...
- PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT 甲级 1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
- PAT 甲级 1011 World Cup Betting (20)(20 分)
1011 World Cup Betting (20)(20 分)提问 With the 2010 FIFA World Cup running, football fans the world ov ...
随机推荐
- Job 和 Cronjob 的使用
Job负责处理任务,即仅执行一次的任务,它保证批处理任务的一个或多个Pod成功结束.而CronJob则就是在Job上加上了时间调度. Job 我们用Job这个资源对象来创建一个任务,我们定一个Job来 ...
- 防HTTP慢速攻击的nginx安全配置
概述 慢速攻击,是一种ddos攻击的变体版本.通常来说,它通过向服务器发送正常的请求,只不过请求的头或者请求体的内容特别长,发送速度有特别慢,这样每一个连接占用的时间就会变得特别长,攻击者会在短时 ...
- Batch - forfiles 命令详解
forfiles 命令详解 C:\Users\cuixunxu>forfiles /? FORFILES [/P pathname] [/M searchmask] [/S] [/C comma ...
- delphi透明panel组件或者制作方法
//透明Panel控件 unit TranPanel; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, ...
- CF540D Bad Luck Island(期望dp)
传送门 解题思路 比较容易的一道期望\(dp\),设\(f[i][j][k]\)表示石头\(i\)个,剪刀\(j\)个,步子\(l\)个.然后转移的时候用组合数算一下就好了. 代码 #include& ...
- NX二次开发-UFUN获取边的光顺性UF_MODL_ask_edge_smoothness(找相切面)
#include <uf.h> #include <uf_modl.h> #include <uf_obj.h> UF_initialize(); //获取面的所有 ...
- NX二次开发-UFUN工程图表格注释获取某一行的tag函数UF_TABNOT_ask_nth_row
NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...
- [luogu 4389] 付公主的背包
题意:求一个较大的多重背包对于每个i的方案数,答案对998244353取模. 思路: 生成函数: 对于一个\(V\) 设: \(f(x) = \sum_{i=0}^{oo} x ^ {V * i} = ...
- O(n)线性时间求解第k大-HDU6040-CSU2078
目录 目录 思路 (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 HDU6040:传送门 \(m(m\leq 100)\)次查询长度为\(n(n \leq 1e7)\)区间的 ...
- Openstack Nova 源码分析 — Create instances (nova-conductor阶段)
目录 目录 前言 Instance Flavor Instance Status Virt Driver Resource Tracker nova-conductor Create Instance ...

