7/22下午

dxy09tj / 2023-08-02 / 原文

1213 八皇后问题

 http://ybt.ssoier.cn:8088/problem_show.php?pid=1213

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

int a[10][10],s=0;
bool b[1000], c[1000],d[1000];

void output(){
    cout<<"No. "<<s<<endl;//注意好是大写还是小写 
    for(int i=1; i<=8; i++){
        for(int j=1; j<=8; j++){
            cout<<a[j][i]<<" ";
        }
        cout<<endl;
    }
}

void dfs(int m){
    for(int j=1; j<=8; j++){
        if(b[j]==false && c[m+j-1]==false && d[m-j+8]==false){
            a[m][j]=1;
            b[j]=true;
            c[m+j-1]=true;
            d[m-j+8]=true;
            if(m==8){
                    s++;
                    output();
            }else {
                dfs(m+1);
            }
            b[j]=false;
            c[m+j-1]=false;
            d[m-j+8]=false;
            a[m][j]=0;//"=="和"="不要搞错啦!!! 
        }
    }
}



int main(){
    dfs(1); 
    return 0;
}