1128. N Queens Puzzle (20)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 思路

N皇后问题,只不过题目仅要求判断是否是同一行和同一对角线。
对于每一个输入的数nums[i]。
1.判断是否和前面的皇后棋子是否在同一行,即输入的第i个数和前i-1个数是否相同。
2.判断是否在同意对角线上,即第i个数和前i-1个数的斜率是否相同,即abs(nums[i] - nums[k]) == abs(i - k) ( 0 <= k < i)是否成立。 代码
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
int main()
{
int K;
while(cin >> K)
{
for(int i = ;i < K;i++)
{
int N;
cin >> N;
vector<int> nums(N);
bool isSolution = true;
for(int j = ;j < N;j++)
{
cin >> nums[j];
for(int k = ;k < j;k++)
{
if(nums[k] == nums[j] || abs(nums[j] - nums[k]) == abs(j - k))
{
isSolution = false;
break;
}
}
}
if(isSolution)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
}

Pat1128:N Queens Puzzle的更多相关文章

  1. 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 ...

  2. PAT 1128 N Queens Puzzle

    1128 N Queens Puzzle (20 分)   The "eight queens puzzle" is the problem of placing eight ch ...

  3. A1128. N Queens Puzzle

    The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...

  4. PAT A1128 N Queens Puzzle (20 分)——数学题

    The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...

  5. PAT甲级 1128. N Queens Puzzle (20)

    1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...

  6. PAT 1128 N Queens Puzzle[对角线判断]

    1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...

  7. PAT 甲级 1128 N Queens Puzzle

    https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...

  8. 1128 N Queens Puzzle (20 分)

    The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...

  9. PAT_A1128#N Queens Puzzle

    Source: PAT A1128 N Queens Puzzle (20 分) Description: The "eight queens puzzle" is the pro ...

随机推荐

  1. How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs

    Goal   Solution   References APPLIES TO: Oracle Supplier Lifecycle Management - Version 12.1.2 and l ...

  2. adb shell后出现error解决方案

    解决办法: 解决办法: 1.adb kill-server 2.adb start-server 3.adb remount 4.adb shell 一般情况下都可以在此启动adb相关

  3. 【55】java异常机制剖析

    一.为什么要使用异常 首先我们可以明确一点就是异常的处理机制可以确保我们程序的健壮性,提高系统可用率.虽然我们不是特别喜欢看到它,但是我们不能不承认它的地位,作用.有异常就说明程序存在问题,有助于我们 ...

  4. Activity之间传递大数据问题

    Android开发人员都知道,Intent适用于在不同的Activity之间传递数据,包括参数.字符串.以及序列化的对象等.但是笔者所做的项目用到了使用Intent 传递Bitmap图片对象,图片的数 ...

  5. 安卓系统启动脚本init.rc说明文件readme.txt翻译

    本说明文件位于system/core/init/readme.txt 本文参考深入解析安卓系统一书,进行翻译,版权部分归书的作者  刘超,资深Android专家,系统架构师. 博客地址:http:// ...

  6. Java Socket:Java-NIO-ServerSocketChannel

    ServerSocketChannel 让我们从最简单的ServerSocketChannel来开始对socket通道类的讨论 ServerSocketChannel是一个基于通道的socket监听器 ...

  7. C# 操作Word 文档——添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  8. window配置mongodb集群(副本集)

    参数解释: dbpath:数据存放目录 logpath:日志存放路径 pidfilepath:进程文件,有利于关闭服务 logappend:以追加的方式记录日志(boolean值) replSet:副 ...

  9. TCP浅谈为什么3次握手

    <计算机网络>中的例子是这样的,"已失效的连接请求报文段"的产生在这样一种情况:客户发出的第一个连接请求报文段并没有丢失,而是在某个网络结点长时间的滞留了,以致延误到连 ...

  10. json的命名空间

    <script>var zgz={}; zgz.common={ getByClass: function () { }, myAddEvent: function () { }}; zg ...