求助!卡死了!
查看原帖
求助!卡死了!
643614
littleTang楼主2022/4/5 15:43

帮忙看一下,谢谢!

#include<iostream>
#include<queue>
using namespace std;
int n,k;
int num[10000];
struct node{
    int data[10000];
    int sum;
    int index;
    bool operator ==(const node rhs) const{
        queue <int> s;
        for(int i=0;i<k;i++){
            s.push(data[i]);
        }
        while(!s.empty()){
            int c=s.front();
            s.pop();
            bool now=1;
            for(int i=0;i<k;i++){
                if(c==rhs.data[i])  now=!now;
            }
            if(now==false)  return 0;
        }
        return 1;
    }
    void sumall(){
        sum=0;
        for(int i=0;i<k;i++){
            sum+=data[i];
        }
    }
};
queue <node> s,tr;
void pd(node now){
    for(int i=0;i<tr.size();i++){
        node c=tr.front();
        tr.pop();
        tr.push(c);
        if(now==c)  return;
    }
    tr.push(now);
}
int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++){
        num[i]=i;
    }
    node head={{0},0,0};
    s.push(head);
    while(!s.empty()){
        node p=s.front();
        s.pop();
        if(p.sum==n){
            bool d=1;
            for(int i=0;i<tr.size();i++){
                node m=tr.front();
                tr.pop();
                tr.push(m);
                if(m==p)    d=0;
            }
            if(d==1){
                tr.push(p);
                continue;
            }
        }
        node ne=p;
        ne.index++;
        s.push(ne);
        for(int i=0;i<k;i++){
            node ne=p;
            ne.index++;
            ne.data[i]+=num[ne.index];
            ne.sumall();
            if(ne.sum<=n)   s.push(ne);
        }
    }
    cout<<tr.size();
    return 0;
}
2022/4/5 15:43
加载中...