听取WA声一片

P1219 [USACO1.5] 八皇后 Checker Challenge

LG_Sam @ 2024-06-20 22:00:47

why 全WA??(悬关求调)

#include<bits/stdc++.h>
using namespace std;
const int N=10;
int n,a[N][N],to[5][2*N],c=0;
bool canAttack(int x,int y){
    return to[1][x]==1||to[2][y]||to[3][x-y+n]==1||to[4][x+y-1]==1;
}
void print(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<c;
}
void search(int t){
    if(t>n){
        print();
        return;
    }
    for(int i=1;i<=n;i++){
        if(canAttack(t,i)) continue;
        a[t][i]=t;
        to[1][t]=1;
        to[2][i]=1;
        to[3][t-i+n]=1;
        to[4][t+i-1]=1;
        c++;
        search(t+1);
        a[t][i]=0;
        to[1][t]=0;
        to[2][i]=0;
        to[3][t-i+n]=0;
        to[4][t+i-1]=0;
        c--;
    }
}
int main(){
    cin>>n;
    search(1);
    return 0;
}

by 123huchenghao @ 2024-06-28 20:36:49

我的AC代码


by 123huchenghao @ 2024-06-28 20:37:00

#include <bits/stdc++.h>
using namespace std;
int heng[20], jia[50], jian[50], put[20];  
int x, ans;  
int n, flag = 1; 
void print() {
    flag++;
    for (int i = 1; i <= n; i++)
        cout << put[i] << " ";
    cout << endl;
    return;
}
void dfs(int x) {
    for (int i = 1; i <= n; i++) {
        if (heng[i] == 0 && jia[i + x] == 0 && jian[i - x + 10] == 0) {
            put[x] = i;
            heng[i] = 1;
            jia[i + x] = 1;
            jian[i - x + 10] = 1;
            if (x == n) {
                ans++;
                if (flag <= 3)  
                    print();
            } else {
                dfs(x + 1);
            }
            heng[i] = 0;  
            jia[i + x] = 0;
            jian[i - x + 10] = 0;
        }
    }
    return;
}
int main() {
    cin >> n;
    dfs(1);  
    cout << ans; 
    return 0;
}

by ouxiyao @ 2024-07-16 16:37:12

@123huchenghao 快撤回,当心······


|