1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 7 struct Node 8 { 9 int mod; //余 10 int digit; //该位的值 11 int pre; //前一个是 12 int step; //第几位了 13 }Q[5001],init={0,0,-1,0};//初始状态 14 15 int n,c,m; 16 int num[16]; // 16 个数 17 int use[5001]; //余 ,标记 18 19 void print(int p) 20 { 21 if (Q[p].pre==-1) 22 return; 23 else 24 print(Q[p].pre); 25 if (Q[p].digit<10) 26 printf("%c",Q[p].digit+‘0‘); 27 else 28 printf("%c",Q[p].digit-10+‘A‘); 29 } 30 31 void bfs() 32 { 33 memset(use,0,sizeof(use)); 34 int head=0,tail=1; 35 Q[0]=init; 36 int ok=0; 37 while (head!=tail) 38 { 39 if (ok) break; 40 Node x =Q[head]; 41 for (int i=0;i<m;i++) 42 { 43 int yu = (x.mod*c+num[i])%n; 44 if (use[yu]||(x.pre==-1&&num[i]==0)||x.step>=500) continue; 45 Q[tail].mod=yu; 46 Q[tail].digit=num[i]; 47 Q[tail].pre=head; 48 Q[tail].step=x.step+1; 49 use[yu]=1; 50 if (yu==0) 51 { 52 print(tail); 53 ok=1; 54 break; 55 } 56 tail++; 57 } 58 head++; 59 } 60 if (ok) 61 printf("\n"); 62 else 63 printf("give me the bomb please\n"); 64 } 65 66 int main() 67 { 68 int T; 69 scanf("%d",&T); 70 while (T--) 71 { 72 scanf("%d%d%d",&n,&c,&m); 73 for (int i=0;i<m;i++) 74 { 75 char tt[2]; 76 scanf("%s",tt); 77 if (tt[0]>=‘0‘&&tt[0]<=‘9‘) 78 num[i]=tt[0]-‘0‘; 79 else 80 num[i]=tt[0]-‘A‘+10; 81 } 82 sort(num,num+m); 83 if (n==0) 84 { 85 if (num[0]==0) 86 printf("0\n"); 87 else 88 printf("give me the bomb please\n"); 89 } 90 else 91 bfs(); 92 } 93 return 0; 94 }
原文:http://www.cnblogs.com/haoabcd2010/p/6370067.html