[SCOI2005]超级格雷码

题目

BZOJ
洛谷

做法

爆搜真有意思

满足不重复且异或后仅一位为\(1\)

利用奇偶性交叉搜索(仅改变一位)

My complete code

#include<bits/stdc++.h>
using namespace std;
typedef int LL;
LL n,B;
LL a[31];
void Dfs(LL now,LL op){
    if(now==n+1){
        for(LL i=1;i<=n;i++)
            if(a[i]>9)
                cout<<(char)(a[i]-10+'A');
            else
                cout<<a[i];
        cout<<endl;
        return;
    }
    if(op==0){
        for(LL i=0;i<B;i++){
            a[now]=i;
            Dfs(now+1,(i%2)?1:0);
        }
    }else{
        for(LL i=B-1;i>=0;i--){
            a[now]=i;
            Dfs(now+1,(i%2)?0:1);
        }
    }
}
int main(){
    cin>>n>>B;
    Dfs(1,0);
    return 0;
} 
posted @ 2019-03-03 20:54  y2823774827y  阅读(187)  评论(0编辑  收藏  举报