HDU 1226

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1226


直接BFS就可以了。每次保留余数,并且判重。当n==0的时候太坑爹了。。错了N次。

下面是AC代码:

#include<iostream>
#include<queue>
#include<string>
using namespace std;
int n,c,m;
int a[20];
int flag;
bool visited[5100];
string ans;
struct node{
    
    string ans;
    int mod;
}now;
void bfs(){
    queue<node >q;
    int i;
    for(i=1;i<16;i++){
        if(a[i]){
            visited[i%n]=1;
            if(i<10){
                
                now.ans="";      now.ans=i+'0';  now.mod=i%n;    q.push(now);
            }
            else{
                now.ans="";      now.ans='A'+i-10 ;  now.mod=i%n;    q.push(now);
                
            }
            
        }
    }
    
    while(!q.empty()){
        now=q.front();  q.pop();
        
        if(flag&&now.ans.size()>ans.size()) continue;
    //    cout<<now.ans<<endl;
        if(now.mod==0){
            if(!flag)  ans=now.ans;
            flag=1;
        

            if(ans.size()>now.ans.size()||(ans.size()==now.ans.size()&&ans>now.ans)){
                ans=now.ans;         
            }
                                                               
        }
        
        for(i=0;i<16;i++){
            if(a[i]){
                if(i<10){
                    node next=now;

                    next.ans+=(i+'0');
                    next.mod=(now.mod*c+i)%n;
                    
                    if((next.ans.size()<=500&&!visited[next.mod])||next.mod==0){
                        visited[next.mod]=1;
                        q.push(next);

                    }
                }
                else{
                    node next=now;
                    next.ans+=('A'+i-10);  
                    next.mod=(now.mod*c+i)%n;
                    
                    if((next.ans.size()<=500&&!visited[next.mod])||next.mod==0){
                        visited[next.mod]=1;
                        q.push(next);
                    }
                    

                }
            }
            
        }
        
    }
}
int main(){
    
    int t,i;
    

    cin>>t;
    
    while(t--){
        char str;
        scanf("%d%d%d",&n,&c,&m);
        getchar();
        memset(a,0,sizeof(a));
        memset(visited,0,sizeof(visited));
        for(i=0;i<m;i++) {
            scanf("%c",&str);
            if(str>='0'&&str<='9'){
                a[str-'0']=1;
            }
            else{
                a[str-'A'+10]=1;
            }
            getchar();
        }
        
  //     for(i=0;i<=16;i++)
    //       if(a[i]==1)
      //        cout<<i<<endl;

        flag=0;  ans="";
        
        if(n==0){
           // cout<<0<<endl;
            if(a[0]==1)
                cout<<0<<endl;
            else
                cout<<"give me the bomb please"<<endl;
            continue;
        }
        
        bfs();
        
        
        if(flag){
            cout<<ans<<endl;
        }
        else
            cout<<"give me the bomb please"<<endl;
    }
    return 0;
    
}


你可能感兴趣的:(c,String)