A1128. N Queens Puzzle
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<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int num[], N, K, hashTB[];
int main(){
scanf("%d", &K);
for(int i = ; i < K; i++){
scanf("%d", &N);
fill(hashTB, hashTB + , );
for(int j = ; j <= N; j++){
scanf("%d", &num[j]);
hashTB[num[j]]++;
}
int tag = ;
for(int j = ; j <= N; j++){
if(hashTB[j] != ){
tag = ;
break;
}
int m = j - , n = num[j] - ;
while(m >= && m <= N && j >= && j <= N){
if(num[m] == n){
tag = ;
break;
}
m--; n--;
}
m = j + ; n = num[j] + ;
while(m >= && m <= N && j >= && j <= N){
if(num[m] == n){
tag = ;
break;
}
m++; n++;
}
}
if(tag == )
printf("NO\n");
else printf("YES\n");
}
cin >> N;
return ;
}
总结:
1、由于已经保证了不在同一列,所以只需要检查行和斜线即可。
2、检查a、b两点间的斜线,可用abs(Xa - Xb) == abs(Ya - Yb)。
A1128. N Queens Puzzle的更多相关文章
- PAT A1128 N Queens Puzzle (20 分)——数学题
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...
- PAT甲级——A1128 N Queens Puzzle【20】
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
- PAT_A1128#N Queens Puzzle
Source: PAT A1128 N Queens Puzzle (20 分) Description: The "eight queens puzzle" is the pro ...
- Poj 3239 Solution to the n Queens Puzzle
1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...
- Pat1128:N Queens Puzzle
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- 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 (20)
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 1128 N Queens Puzzle[对角线判断]
1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...
- PAT 甲级 1128 N Queens Puzzle
https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...
随机推荐
- Laravel 5.6 模型关联 user 表后查询 user 表数据只能获取第一条数据,不知道怎么获取第二条
按照开发手册的说法,肯定是指令不够全,附代码图 如果, tests 是文章表, users 是用户表 test.com/tests/1 是 id 为 1 的文章地址( get 访问) 假如 Tests ...
- 用mescroll实现无限上拉增加数据,下拉刷新数据 (学习笔记)
最近自己做一个web app需要用到上拉查询下页数据,网上看了很多很多帖子,发现并不能快速的套用,总是会出现各种问题无法使用,于是无奈自己跑去看了官方api文档,终于做了出来,至此做个笔记,以后用到可 ...
- Yii2的save()方法容易出错的地方
如果save()返回true, 但是数据没有保存成功,则应该是开启了事务且已经回滚 如果save()返回false, 则使用$model->errors查看错误原因 可以设置$model的场景, ...
- Java HashMap的put操作(Java1.6)
https://www.cnblogs.com/skywang12345/p/3310835.html // 存储数据的Entry数组,长度是2的幂. // HashMap是采用拉链法实现的,每一个E ...
- orcale增加列脚本
--编号declare v_cnt number; V_SQL VARCHAR2 (500) := '';begin select count(*) into v_cnt from dual wher ...
- Jenkins+PowerShell持续集成环境搭建(四)常用PowerShell命令
0. 修改执行策略 Jenkins执行PowerShell脚本,需要修改其执行策略.以管理员身份运行PowerShell,执行以下脚本: Set-ExecutionPolicy Unrestricte ...
- [离散时间信号处理学习笔记] 9. z变换性质
z变换描述 $x[n] \stackrel{\mathcal{Z}}{\longleftrightarrow}X(z) ,\quad ROC=R_x$ 序列$x[n]$经过z变换后得到复变函数$X(z ...
- Nginx TSL/SSL优化握手性能
L:131
- IntelliJ cannot log in to GitHub上传github报错解决
重装系统,新装的Intellij IDEA上新建的项目上传github失败,报错: invalid authentication token ... 此处多为本地git用户的用户名/邮箱,与之前设置的 ...
- Android Spinner 绑定键值对
这里给大家提供下绑定 spinner键值对的方法. 首先创建绑定模型BaseItem public class BaseItem { public BaseItem(Integer id,String ...