[PAT] Grade 1128 N Queens Puzzle (20 minutes)

Meaning of the questions:

Enter a positive integer K (<= 200), followed by K input lines, the first number is N (<= 1000), then followed by N integers (1 ~ N) represents the line number of the i-th column Queen, all Queen is not a, whether the output of all the Queen's not attack each other.

trick:

N ^ 2/2 does not time out, N ^ 2 times out the last test point.

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int a[1007];
 5 int main(){
 6     ios::sync_with_stdio(false);
 7     cin.tie(NULL);
 8     cout.tie(NULL);
 9     int t;
10     cin>>t;
11     while(t--){
12         int n;
13         cin>>n;
14         int flag=0;
15         for(int i=1;i<=n;++i){
16             cin>>a[i];
17             for(int j=1;j<i;++j){
18                 int temp=i-j;
19                 int x=a[i]+temp;
20                 int y=a[i]-temp;
21                 if(a[j]==x||a[j]==y||a[j]==a[i])
22                     flag=1;
23             }
24         }
25         if(flag)
26             cout<<"NO\n";
27         else
28             cout<<"YES\n";
29     }
30     return 0;
31 }

 

Guess you like

Origin www.cnblogs.com/ldudxy/p/12541659.html