PAT_A1128#N Queens Puzzle
Source:
Description:
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 Klines 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; orNO
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
Keys:
- 简单模拟
- 八皇后问题
Attention:
- 给出的N个皇后可能在同一行
Code:
/*
Data: 2019-05-25 10:24:05
Problem: PAT_A1128#N Queens Puzzle
AC: 18:04 题目大意:
判断给定的序列是否为N皇后问题的解
输入:
第一行给出:测试数K<=200;
接下来K行,问题规模N<=1e3,N个数
输出:
Yes or No
*/ #include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e3+; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,m;
scanf("%d", &m);
while(m--)
{
scanf("%d", &n);
int q[M],ans=;
for(int i=; i<=n; i++)
scanf("%d", &q[i]);
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
if(abs(j-i)==abs(q[j]-q[i]) || q[j]==q[i]){
ans=;break;
}
if(ans==)
break;
}
if(ans) printf("YES\n");
else printf("NO\n");
} return ;
}
PAT_A1128#N Queens Puzzle的更多相关文章
- 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 ...
- A1128. N Queens Puzzle
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×8 chessboar ...
- 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 ...
- 1128 N Queens Puzzle (20 分)
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
随机推荐
- Clojure:ZeroMQ的入门DEMO
假设你已经知道什么是ZeroMQ(不知道的话可以看这个:http://zh.wikipedia.org/wiki/%C3%98MQ),以下就给出在Clojure中如何使用ZeroMQ(感谢此文作者:h ...
- Java修改文件夹名称
Java修改文件夹名称 学习了:http://blog.csdn.net/yongh701/article/details/45063833 进行文件夹名字批量修改,注意,要写全路径: package ...
- maven的启动类和MAVEN_OPTS参数
maven的启动类和MAVEN_OPTS参数 在mvn.cmd的155行, set CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launc ...
- Rails 教程
Rails 教程 http://web.siwei.tech/ http://www.siwei.me/
- umask函数的使用方法 - 怎样进行权限位的设置
以下程序创建了两个文件,创建foo文件时,umask值为0,创建第二个时,umask值禁止全部组和其它用户的訪问权限. 測试结果: 測试结果能够看出更改进程的文件模式掩码并不影响其父进程(经常是she ...
- Linuxpassword破解及grub加密演示
password破解及grub加密演示 so easy,不可不会! 原理: 通过进入单用户模式(单用户模式也即是仅仅有一个用户能够訪问资源的状态,且单用户模式就是系统处于最原始的状态,大部分服务还未开 ...
- iOS - 社会化分享-微信分享,朋友圈分享
我仅仅做了文字和图片分享功能 1. TARGETS - Info - URL Types identifier -> weixin URL Schemes -> 应用id 2.在AppD ...
- ip地址转换成16进制long
<span style="font-size:18px;">public class IpUtil { /** * ip地址转换成16进制long * @param i ...
- USACO money packageDP
裸0/1背包,就是从各种币种里面拿来凑足N元,求最多有多种方案.用dp[i][j]表示选前i个币种凑成j的方案数量 状态转移方程: dp[i][j] = dp[i- 1][j] j < c ...
- commons-fileupload上传文件(1)
近期,写一个上传图片的功能.于是用到commons-fileupload这个组件.提过form提交表单到后台(这里没实用到structs框架).在后台List pl = dfu.parseReques ...