0分求调

P1219 [USACO1.5] 八皇后 Checker Challenge

__why @ 2023-11-24 18:27:12

#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
bool d[maxn*maxn] = {0},b[maxn] = {0},c[maxn*maxn] = {0};
int n,sum = 0,a[100005];
void print(){
    if(sum==3);
    sum++;
    for(int i = 1;i<=n;i++)cout<<a[i]<<" ";
    cout<<endl;
}
int search(int i){
    for(int j = 1;j<=n;j++){
        if((!b[j])&&(!c[i+j])&&(!d[i-j+n-1])){
            a[i] = j;
            b[j] = 1;
            c[i+j] = 1;
            d[i-j+n-1] = 1;
            if(i==n)print();
            else {
                search(i+1);
                a[i] = 0;
                b[j] = 0;
                c[i+j] = 0;
                d[i-j+n-1] = 0;
            }

        }
    }
}
int main(){
    cin>>n;
    search(1);
    cout<<sum;
    return 0;
}

by clx201022 @ 2023-11-24 21:35:16

改好了

#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
bool d[maxn<<1] = {0},b[maxn] = {0},c[maxn<<1] = {0};//开那么大有什么意义啊喂
int n,sum = 0,a[maxn+1];
void print(){
    sum++;
    if(sum>3)return ;//你忘打 return 了
    for(int i=1;i<=n;i++)cout<<a[i]<<" ";
    cout<<endl;
}
void search(int i){
    for(int j=1;j<=n;j++)
    {
        if(b[j]||c[i+j]||d[i+n-j-1])
            continue;
        else
        {
            a[i] = j;
            b[j] = 1;
            c[i+j] = 1;
            d[i+n-j-1] = 1;
            if(i>=n)
                print();
            else
                search(i+1);
            a[i] = 0;//到 n 个 也要摆回去!!!
            b[j] = 0;
            c[i+j] = 0;
            d[i+n-j-1] = 0;

        }
    }
}
int main(){
    cin>>n;
    search(1);
    cout<<sum<<'\n';
    return 0;
}

by clx201022 @ 2023-11-24 21:36:44

@wuhaoyu15
if(sum==3)return;

if(i==n) print();
else search(i+1);
a[i] = 0;
b[j] = 0;
c[i+j] = 0;
d[i-j+n-1] = 0;

by clx201022 @ 2023-11-24 21:38:30

@clx201022


by __why @ 2023-11-25 08:12:12

谢谢 @clx201022


|