小白求问:为什么N后问题中不需要考虑x+y=t这条直线?
n后问题:
#include<iostream>
#include<cmath>
using namespace std;
#define N 8
int a[N+1] = { 0 };
int sum;
bool place(int t)
{
for (int i = 1; i < t; i++)
{
if (abs(i - t) == abs(a[i] - a[t]) || a[i] == a[t])
return false;
}
return true;
}
void trial(int t)
{
if (t > N)
{
for (int i = 1; i <= N; i++)
cout << a[i] << " ";
cout << endl;
sum++;
}
else
{
for (int i = 1; i <= N; i++)
{
a[t] = i;
if (place(t))
trial(t + 1);
}
}
}
int main()
{
trial(1);
cout << sum << endl;
return 0;
}
源代码如图所示,运行正确,但是我想请问下为什么place函数中只考虑了y-x=t这条直线,不考虑y+x=t这条直线,就是
if (abs(i - t) == abs(a[i] - a[t]) || a[i] == a[t])这个表达式为什么不改成
if (abs(i - t) == abs(a[i] - a[t]) || a[i] == a[t]||abs(i+t)==abs{a[i]+a[t]))
为什么加上之后就错了,一直很疑问,求大神解惑。