c++二维数组标记深搜WA求调,玄关

P1219 [USACO1.5] 八皇后 Checker Challenge

miffy_123 @ 2023-11-05 21:07:35

下面是本蒟蒻的代码 十分侮辱各位dalao的眼睛

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 2147483646
#define inf 9223372036854775806
#define rep(i,o,p) for(int (i)=(o);(i)<=(p);(i)++)
#define per(i,o,p) for(int (i)=(o);(i)>=(p);(i)--)
#define pl putchar('\n');
int read(){
    int f=1,k=0;
    char c;
    c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-'){
            f=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        k=(k<<1)+(k<<3)+(c^48);
        c=getchar();
    }
    return f*k;
}
void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}
int a[20],n;
bool ok[20][20];
int ans=0;
void dfs(int x){
    if(x>n){
        if(ans<3){
            rep(i,1,n){
                write(a[i]);
                putchar(' ');
            }
            pl
        }
        ans++;
        return;
    }
    rep(i,1,n){
        if(ok[x][i]){
            int k1=x,k2=i;
            while(k1&&k2){
                k1--;
                k2--;
            }
            k1++;
            k2++;
            while(k1>0&&k2>0&&k1<=n&&k2<=n){
                ok[k1][k2]=false;
                k1++;
                k2++;
            }
            k1=x;
            k2=i;
            while(k1>0&&k2<=n){
                k1--;
                k2++;
            }
            k1++;
            k2--;
            while(k1>0&&k2>0&&k1<=n&&k2<=n){
                ok[k1][k2]=false;
                k1++;
                k2--;
            }
            rep(k,1,n){
                ok[x][k]=false;
            }
            rep(k,1,n){
                ok[k][i]=false;
            }
            a[x]=i;
            dfs(x+1);
            k1=x;
            k2=i;
            while(k1&&k2){
                k1--;
                k2--;
            }
            k1++;
            k2++;
            while(k1>0&&k2>0&&k1<=n&&k2<=n){
                ok[k1][k2]=true;
                k1++;
                k2++;
            }
            k1=x;
            k2=i;
            while(k1>0&&k2<=n){
                k1--;
                k2++;
            }
            k1++;
            k2--;
            while(k1>0&&k2>0&&k1<=n&&k2<=n){
                ok[k1][k2]=true;
                k1++;
                k2--;
            }
            rep(k,1,n){
                ok[x][k]=true;
            }
            rep(k,1,n){
                ok[k][i]=true;
            }
        }
    }
    return;
}
int main(){
    n=read();
    rep(i,1,n){
        rep(j,1,n){
            ok[i][j]=true;
        }
    } 
    dfs(1);
    write(ans);
    return 0;
  //若各位dalao看出问题,对本题有帮助的话,可以私信或在本帖@我。
}

by miffy_123 @ 2023-11-05 21:25:42

更新一下代码

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 2147483646
#define inf 9223372036854775806
#define rep(i,o,p) for(int (i)=(o);(i)<=(p);(i)++)
#define per(i,o,p) for(int (i)=(o);(i)>=(p);(i)--)
#define pl putchar('\n');
int read(){
    int f=1,k=0;
    char c;
    c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-'){
            f=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        k=(k<<1)+(k<<3)+(c^48);
        c=getchar();
    }
    return f*k;
}
void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}
int a[20],n;
bool ok[20][20];
bool ok2[20][20];
int ans=0;
void dfs(int x){
    if(x>n){
        if(ans<3){
            rep(i,1,n){
                write(a[i]);
                putchar(' ');
            }
            pl
        }
        ans++;
        return;
    }
    rep(i,1,n){
        if(ok[x][i]){
            rep(j1,1,n){
                rep(j2,1,n){
                    ok2[j1][j2]=ok[j1][j2];
                }
            }
            int k1=x,k2=i;
            while(k1&&k2){
                k1--;
                k2--;
            }
            k1++;
            k2++;
            while(k1>0&&k2>0&&k1<=n&&k2<=n){
                ok[k1][k2]=false;
                k1++;
                k2++;
            }
            k1=x;
            k2=i;
            while(k1>0&&k2<=n){
                k1--;
                k2++;
            }
            k1++;
            k2--;
            while(k1>0&&k2>0&&k1<=n&&k2<=n){
                ok[k1][k2]=false;
                k1++;
                k2--;
            }
            rep(k,1,n){
                ok[x][k]=false;
            }
            rep(k,1,n){
                ok[k][i]=false;
            }
            a[x]=i;
            dfs(x+1);
            rep(j1,1,n){
                rep(j2,1,n){
                    ok[j1][j2]=ok2[j1][j2];
                }
            }
        }
    }
    return;
}
int main(){
    n=read();
    rep(i,1,n){
        rep(j,1,n){
            ok[i][j]=true;
        }
    } 
    dfs(1);
    write(ans);
    return 0;
}

by flh2011 @ 2023-11-12 12:00:24

需要写这么长吗(虽然我也没过


|