Time Limit: 20000/10000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 2108 Accepted
Submission(s): 669
1 #include <iostream> 2 #include <math.h> 3 #include <algorithm> 4 #include <stdio.h> 5 #include <string.h> 6 #include <queue> 7 using namespace std; 8 int n,c,m; 9 char a[20]; 10 int p[50000]; 11 int funa(char s) 12 { 13 if(s>=‘0‘&&s<=‘9‘)return s-‘0‘; 14 else return s-‘A‘+10; 15 } 16 int fun(string d) 17 { 18 if(d.size()>=505) 19 return -1; 20 int i; 21 int yu=0; 22 for(i=0; i<d.size(); i++) 23 { 24 yu=(yu*c+funa(d[i]))%n; 25 } 26 return yu; 27 } 28 void bfs() 29 { 30 int i; 31 memset(p,0,sizeof(p)); 32 queue<string> z; 33 while(!z.empty())z.pop(); 34 string x; 35 for(i=0; i<m; i++) 36 { 37 if(a[i]==‘0‘)continue; 38 x=a[i]; 39 int r=fun(x); 40 if(!p[r]) 41 { 42 z.push(x); 43 p[r]=1; 44 } 45 if(p[0]) 46 { 47 cout<<a[i]<<endl; 48 return ; 49 } 50 } 51 while(!z.empty()) 52 { 53 x=z.front(); 54 for(i=0; i<m; i++) 55 { 56 int r=fun(x+a[i]); 57 if(r==-1)break; 58 if(!p[r]) 59 { 60 z.push(x+a[i]); 61 p[r]=1; 62 if(p[0]) 63 { 64 cout<<x+a[i]<<endl; 65 return ; 66 } 67 } 68 } 69 z.pop(); 70 } 71 cout<<"give me the bomb please"<<endl; 72 } 73 int main() 74 { 75 int t,i; 76 cin>>t; 77 while(t--) 78 { 79 cin>>n>>c>>m; 80 for(i=0; i<m; i++)cin>>a[i]; 81 sort(a,a+m); 82 if(n==0) 83 { 84 if(a[0]==‘0‘) 85 cout<<0<<endl; 86 else cout<<"give me the bomb please"<<endl; 87 } 88 else 89 bfs(); 90 } 91 }
超级密码 hdu1226 bfs,布布扣,bubuko.com
原文:http://www.cnblogs.com/ERKE/p/3609079.html