• 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考试中,可以取个巧;不用安装输入完成后再全部输出的模式进行。也可以输入一行立刻输出一行结果。也不影响最终的评判。
  • 相关阅读:
    leetcode题目19.删除链表的倒数第N个节点(中等)
    Android学习笔记---使用Service模仿下载效果
    Android学习笔记-简单聊天界面的实现
    Android学习笔记-ContentProvider操作
    Android高级-Android操作SQL数据管理,增删改查
    Android高级-正则表达式
    Android高级-SQL语言
    Android学习之路-录音功能实现
    gitee上传下载代码命令
    opencv进行视频播放每帧处理,读取视频失败
  • 原文地址:https://www.cnblogs.com/bobyin/p/10600504.html
Copyright © 2020-2023  润新知