VJ_N皇后问题_DFS_打表

//
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>           // memset
using namespace std;

const int FF=55;            // hash_F
const int N=111;
bool xx[N],pp[N],nn[N];     // x轴 主对角线 次对角线 —— xoy轴的截距
int out[N];                 // 打表 不打表会超时
int n,ans;

void dfs( int yy )
{
    if( yy>=n ) { ans++; return ; }

    for( int i=0;i<n;i++ )
    {
        if( xx[i] || pp[ FF+i-yy ] || nn[ i+yy ] ) continue ;

        xx[i]=1;
        pp[ FF+i-yy ]=1;
        nn[ i+yy ]=1;

        dfs( yy+1 );

        xx[i]=0;
        pp[ FF+i-yy ]=0;
        nn[ i+yy ]=0;
    }
}

void solve()
{
    for( int i=1;i<=11;i++ )
    {
        memset( xx,0,sizeof( xx ) );
        memset( pp,0,sizeof( pp ) );
        memset( nn,0,sizeof( nn ) );

        n=i;
        ans=0;
        dfs( 0 );
        out[i]=ans;
    }
}

int main()
{
	solve();
    while( cin>>n && n )
    {
        cout<<out[n]<<endl;
    }
    return 0;
}
// |--->y
// |
// X

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/124669307