求调,为啥我这个跑不了

P1219 [USACO1.5] 八皇后 Checker Challenge

qinziao @ 2024-08-30 20:02:08

#include<bits/stdc++.h>
using namespace std;
int n,ans;
bool lie[15],xieh[30],xie1[15],xie2[15];
stack<int>q;
void getout(){
    ans++;
    if(ans>3) return;
    while(q.size()){
        printf("%d ",q.top());
        q.pop();
    }
    printf("\n");
}
bool dfs(int x){
    if(x==n+1) return 1;
    for(int j=1;j<=n;j++)

        if(!lie[j]&&!xieh[x+j]){

            lie[j]=1;
            xieh[x+j]=1;

                if(x>j)
                    if(!xie1[x-j]){

                            xie1[x-j]=1;

                        if(dfs(x+1)){
                            q.push(j);
                            if(x==1) 
                                getout();
                            else
                                return 1;
                        }

                            xie1[x-j]=0;

                    }

                else
                    if(!xie2[j-x]){

                            xie2[j-x]=1;

                        if(dfs(x+1)){
                            q.push(j);
                            if(x==1) 
                                getout();
                            else
                                return 1;
                        }

                            xie2[j-x]=0;

                    }

            lie[j]=0;
            xieh[x+j]=0;

        }

    return 0;
}
int main(){
    cin>>n;
    dfs(1);
    printf("%d\n",ans);
    return 0;
}

by _Vistion_ @ 2024-09-03 14:28:12


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int m1[30], m2[30], m3[30], ans[15], mark=0;
void setvalue(int x, int y, int flag){
    ans[x] = y;
    m1[y] = flag;
    m2[x+y] = flag;
    m3[x-y+n] = flag;
}
void dfs(int step){
    if(step>n){
        mark++;
        if(mark<=3){
            for(int i=1; i<=n; i++){
                cout<<ans[i]<<" ";
            }
            cout<<endl;
        }
        return;
    }
    for(int j=1; j<=n; j++){
        if(m1[j] || m2[step+j] || m3[step-j+n]) continue;
        setvalue(step,j,1);
        dfs(step+1);
        setvalue(step,j,0);
    }
}
int main(){
    cin>>n;
    dfs(1);
    cout<<mark;
    return 0;
}

by hehe_666 @ 2024-09-06 10:00:33

@qinziao ```c

include<cstdio>

include<iostream>

include<algorithm>

using namespace std;

int a[55], n, k, cnt; bool hang[55], lie[55], d1[55], d2[55];

void dfs(int step) { if(step > n) { if(cnt <= 2) { for(int i = 1; i <= n; i++) { cout << a[i] << " "; } cout << endl; } cnt++;

}
for(int i = 1; i <= n; i++)
{
    int x = step, y = i;
    if(hang[x] || lie[y] || d1[x - y + n] || d2[x + y]) continue;
    hang[x] = lie[y] = d1[x - y + n] = d2[x + y] = true;
    a[step] = i;
    dfs(step + 1);
    hang[x] = lie[y] = d1[x - y + n] = d2[x + y] = false;
}

}

int main() { cin >> n; k = 3; dfs(1); cout << cnt; return 0; }


|