不是很能理解这道题的输出样例,为什么我的输出是这样的
1 3 5 7 2 4
1 4 7 3 6 2
1 5 2 6 3 7
4
这个输出为什么和我的不一样?代码在下面
#include <bits/stdc++.h>
using namespace std;
int r [ 15 ] , c [ 15 ] , cnt , n , sum; // r:行 c:列
bool check ( int id )
{
for ( int i = 1; i < id; i++ )
{
if ( r [ i ] == r [ id ] )
{
return 0;
}
if ( c [ i ] == c [ id ] )
{
return 0;
}
if ( abs ( r [ i ] - r [ id ] ) == abs ( c [ i ] - c [ id ] ) )
{
return 0;
}
}
return 1;
}
void dfs ( int id ) // 当前在id行
{
if ( id == n + 1 )
{
cnt++;
if ( sum < 3 )
{
for ( int i = 0; i < n; i++ )
{
cout << c [ i ] + 1 << " ";
}
cout << "\n";
sum++;
}
return;
}
for ( int i = 1; i <= n; i++ )
{
r [ id ] = id , c [ id ] = i;//摆上棋子
if ( check ( id ) == 1 )//检查
{
dfs ( id + 1 );
}
}
}
int main()
{
cin >> n;
dfs ( 1 );
cout << cnt << "\n";
return 0;
}
普通的 “n皇后”是能过的,就是这个列数的问题