diagonal 斜线的 斜对角线的
思路
判断n皇后解是否成立,直线(值相等)和斜线(下标差和值之差的绝对值相等)
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int k, n;
int num[maxn];
int main() {
cin>>k;
while (k--) {
cin>>n;
map<int, int> mp;
int flag = 0;
for (int i = 1; i <= n; i++) {
cin>>num[i];
if (mp.find(num[i]) == mp.end()) {
mp[num[i]] = 1;
} else {
flag = 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (abs(i - j) == abs(num[i] - num[j])) {
flag = 1;
break;
}
}
}
if (flag == 1) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}