第七章八皇后问题

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxn=100;
int vis[3][maxn];
int n;
int tot=0;

void search(int cur)
{
    if(cur==n) tot++;
    else 
        for(int i=0;i<n;i++)
            if(!vis[0][i] && !vis[1][cur+i] && !vis[2][cur-i+n])
            {
                vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=1;
                search(cur+1);
                vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=0;
            }
}

int main()
{
    memset(vis,0,sizeof(vis));

    cin>>n;

    search(0);

    printf("%d
",tot);

    return 0;
}

看了看以前的shceme版也好有趣

Yosoro
原文地址:https://www.cnblogs.com/tclan126/p/7428021.html