题目
思路
标识矩阵的每一条主对角线(y - x + n)和副对角线(y + x),具体跟八皇后问题一样;
代码
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010;
bool row[N], dg[N * 2], udg[N * 2];
int main()
{
int m, n, a[N];
scanf("%d", &m);
for(int i = 0; i < m; i++) {
memset(row, false, sizeof(row));
memset(dg, false, sizeof(dg));
memset(udg, false, sizeof(udg));
scanf("%d", &n);
for(int j = 1; j <= n; j++) scanf("%d", &a[j]);
int ok = 0;
for(int col = 1; col <= n; col++){
if(!row[a[col]] && !dg[col - a[col] + n] && !udg[col + a[col]]) {
row[a[col]] = dg[col - a[col] + n] = udg[col + a[col]] = true;
}
else{
ok = 1; break;
}
}
if(ok == 0) printf("YES\n");
else printf("NO\n");
}
system("pause");
return 0;
}