【PAT甲级 - C++题解】1128 N Queens Puzzle

简介: 【PAT甲级 - C++题解】1128 N Queens Puzzle

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


微信截图_20230618202637.png


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 组数据,每组数据代表一个棋盘上皇后的分布情况,需要我们判断每个棋盘是否满足八皇后问题,即同一行、同一列和对角巷上不能同时存在两个及以上的皇后。


这样我们就可以用一个整数序列 Q1,Q2,…,QN 来表示一种棋盘摆放,其中 Qi 表示第 i 列的皇后所在的行号。


例如,图1的棋盘摆放可以用 (4, 6, 8, 2, 7, 1, 3, 5) 来表示,它是解决八皇后问题的一种合理摆放方案。


图2的棋盘摆放可以用 (4, 6, 7, 2, 8, 1, 9, 5, 3) 来表示,它并不是解决九皇后问题的一种合理摆放方案。


思路

我们可以通过三个数组来表示每一行,及正对角线上和反对角线上是否出现冲突情况。因为题目是按照每一列皇后给出的,所以不用判断每一列上是否同时存在皇后。


而每一行的判断方法就是每当传入一个皇后行的坐标,就将对应位置置为 1 ,如果已经为 1 ,则说明有冲突。


通过观察可以发现,在正对角线上的所有点的横坐标 x 与纵坐标 y 之和即 x+y 的值都相等,所以可以采用上面的方法进行冲突判断。


而在反对角线上的所有点的横坐标 x 与纵坐标 y 之差的值相等,但是可能会出现负数,无法用数组下标表示,所以还需要加上一个偏移量 n 使之变成正数再继续判断。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
bool row[N], dg[N * 2], udg[N * 2]; //对角线需要2*N
int main()
{
    int n;
    cin >> n;
    //判断每一组数据
    for (int i = 0; i < n; i++)
    {
        //初始化数组
        memset(row, 0, sizeof row);
        memset(dg, 0, sizeof dg);
        memset(udg, 0, sizeof udg);
        //检验冲突
        int m;
        cin >> m;
        bool success = true;
        for (int y = 1; y <= m; y++)
        {
            int x;
            cin >> x;
            //判断是否有冲突
            if (row[x] || dg[x + y] || udg[y - x + m]) success = false;
            row[x] = dg[x + y] = udg[y - x + m] = true;
        }
        //输出判断结果
        if (success) puts("YES");
        else puts("NO");
    }
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
64 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
81 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
75 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
73 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
76 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
76 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
128 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
55 0
|
存储 人工智能 C++
【PAT甲级 - C++题解】1085 Perfect Sequence
【PAT甲级 - C++题解】1085 Perfect Sequence
70 0
|
C++
【PAT甲级 - C++题解】1046 Shortest Distance
【PAT甲级 - C++题解】1046 Shortest Distance
65 0