为啥不行呢

P1219 [USACO1.5] 八皇后 Checker Challenge

songshihao666888 @ 2023-07-12 10:49:59

#include<iostream>
using namespace std;
const int N=10;
int n,ans[N],pa=0,cnt=0;
bool col[N],dg[N],udg[N];
char g[N][N];
void dfs(int u){
    if(u==n){
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(g[i][j]=='Q')ans[pa++]=j+1;
        if(cnt<3){
            for(int i=0;i<pa;i++){
                cout<<ans[i]<<" ";
            }
            if(cnt<3)puts("");
        }
        cnt++;
        pa=0;
        return;
    }
    for(int i=0;i<n;i++){
        if(!col[i]&&!dg[u+i]&&!udg[i-u+n]){
            g[u][i]='Q';
            col[i]=true,dg[i+u]=true,udg[i-u+n]=true;
            dfs(u+1);
            g[u][i]='.';
            col[i]=false,dg[i+u]=false,udg[i-u+n]=false;
        }
    }
}  
signed main(){
    cin>>n;
    for(int i=0;i<n;i++) 
        for(int j=0;j<n;j++)
            g[i][j]='.';
    dfs(0);
    cout<<cnt;
}

by WangSH2012 @ 2023-07-18 21:03:23

用万能头

include<bits/stdc++.h>


|