• PAT 1128 N Queens Puzzle


    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×Nchessboard. (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.

    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 "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
    

      

    结果

     1 #include<cstdio>
     2 #include<vector>
     3 #include<algorithm>
     4 using namespace std;
     5 //const int maxk = 210;
     6 const int maxn = 1010;
     7 int k,n,a[maxn] ={0};
     8 
     9 bool isOK(int a[], int n){
    10     bool flag = true;
    11     for(int i=1;i<n;i++){
    12         for(int j=i+1;j<=n;j++){
    13             if(a[i] == a[j] or abs(i-j) == abs(a[i]-a[j])){
    14                 flag = false;
    15                 return flag;
    16             }
    17         }
    18     }
    19     return flag;
    20 }
    21 
    22 int main(){
    23     scanf("%d",&k);
    24     for(int i=0;i<k;i++){
    25         // input data;
    26         scanf("%d",&n);
    27         for(int j=1;j<=n;j++){
    28             scanf("%d",&a[j]);
    29         }
    30         // get result,and print result
    31         bool result;
    32         result = isOK(a,n);
    33         if(result) printf("YES");
    34         else printf("NO");
    35         if(i<k-1) printf("
    ");
    36     }
    37     return 0;
    38 }


    备注:
    1)写的时候,我发现在PAT考试中,可以取个巧;不用安装输入完成后再全部输出的模式进行。也可以输入一行立刻输出一行结果。也不影响最终的评判。
  • 相关阅读:
    JS—超酷时钟
    JS—简单年历表
    ZZULI 1783 简单的求和
    ZZULI 1788 小金刚的宝藏(01背包模板)
    HDU 5172 GTY's gay friends(BestCoder Round #29)
    HDU 2157 How many ways??
    HDU 5171 GTY's birthday gift(BestCoder Round #29)
    在SQL Server 中启用 FileStream
    (转载)SQL Server的一个不显眼的功能 备份文件的分割
    面向对象建模方法与数据库建模方法的比较
  • 原文地址:https://www.cnblogs.com/bobyin/p/10600504.html
走看看 - 开发者的网上家园