代码实现
1.
只要当前列、主副对角线有其他棋子,那就不行
#include <iostream>
using namespace std;
const int N = 1010;
int k;
int main()
{
cin >> k;
while (k--)
{
int n;
cin >> n;
bool cal[n + 1] = {false}, dg[n * 2 + 1] = {false}, udg[n * 2 + 1] = {false}, f = true;
for (int x = 1; x <= n; x++)
{
int y;
cin >> y;
if (cal[y] || dg[y + x] || udg[y - x + n]) f = false;
else cal[y] = dg[y + x] = udg[y - x + n] = true;
}
if (f) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
2.
行与行之间两两比较,要是列相同或者列与列差和行与行差相等(正好俩棋子处于一个正方形的对角线上,k = 1),那就NO
#include <iostream>
using namespace std;
const int N = 10010;
int k, path[N];
bool check(int n)
{
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (path[i] == path[j] || abs(i - j) == abs(path[i] - path[j]))
return false;
return true;
}
int main()
{
cin >> k;
while (k--)
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
scanf("%d", &path[i]);
if (check(n)) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}