样例死活都只输出0

P1219 [USACO1.5] 八皇后 Checker Challenge

diaq @ 2023-07-02 19:43:41

dalao们。。。

#include<bits/stdc++.h>
using namespace std;
int line[14],n,ans;
void print(){
    for(int i=1;i<=n;i++)cout<<line[i]<<" ";
}
bool check(int want){
    for(int i=1;i<=want;i++){
        if(line[want]==line[i])return true;
        if(line[want]-want==line[i]-i)return true;
        if(line[want]+want==line[i]+i)return true;
    }
    return false;
}
void dfs(int step){
    for(;line[step]<n;line[step]++){
        if(check(step))continue;
        else{
            if(step!=n)dfs(step+1);
            else{
                if(ans<=3)print();
                ans++;
            }
        }
    }
}
int main(){
    cin>>n;
    dfs(1);
    cout<<ans;

    return 0;
}

by vicky2048_2 @ 2023-07-02 20:16:06

没仔细看

盲猜你的for(;line[step]<n;line[step]++) 或者 check的判断写错了捏QwQ


by vicky2048_2 @ 2023-07-02 20:18:23

@diaq


by Maysoul @ 2023-07-02 20:19:06

#include<bits/stdc++.h>
using namespace std;
int line[14],n,ans;
void print(){
    for(int i=1;i<=n;i++)cout<<line[i]<<" ";
    cout<<endl;
}
bool check(int want){
    for(int i=1;i<want;i++){
        if(line[want]==line[i])return true;
        if(line[want]-want==line[i]-i)return true;
        if(line[want]+want==line[i]+i)return true;
    }
    return false;
}
void dfs(int step){
    for(int i=1;i<=n;i++){
        line[step]=i;
        if(check(step))continue;
        else{
            if(step!=n)dfs(step+1);
            else{
                if(ans<3)print();
                ans++;
            }
        }
    }
}
int main(){
    cin>>n;
    dfs(1);
    cout<<ans;
    return 0;
}

只改动了check函数里面循环条件,for(;line[step]<n;line[step]++这一句改掉了(for(int i=1;i<=n;i++)是好文明)最后不要忘了换行


by diaq @ 2023-07-04 15:15:48

aaa谢谢:D


by diaq @ 2023-07-04 15:25:47

@Maysoul 可是这样的话#8超时了欸T.T


by Maysoul @ 2023-07-04 15:26:51

@diaq 吸氧

或者把cout<<endl;改成输出换行符


|