8皇后问题
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=25;
int vis[maxn][maxn],used[maxn][maxn],n,cnt=0;
void dfs(int tot){
int i,j;
if(tot==n+1){
cnt++;printf("No %d:\n",cnt);
for(i=1;i<=8;i++){
for(j=1;j<=8;j++){
if(!used[i][j]){
cout<<'.';
}
else{
cout<<'A';
}
}
cout<<endl;
}
return ;
}
for(i=1;i<=8;i++){
if(vis[0][i]||vis[1][tot+i-1]||vis[2][tot-i+n])continue;
used[tot][i]=1;vis[0][i]=vis[1][tot+i-1]=vis[2][tot-i+n]=1;
dfs(tot+1);
used[tot][i]=0;vis[0][i]=vis[1][tot+i-1]=vis[2][tot-i+n]=0;
}
}
int main(){
n=8;
dfs(1);
return 0;
}