hdu-超级密码(BFS)
生活随笔
收集整理的這篇文章主要介紹了
hdu-超级密码(BFS)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1399
學(xué)了c++感覺有了用武之地用string存儲字符串,感覺很方便;
題意:給你m個數(shù)字用任意數(shù)字組合成n的最小倍數(shù),此題用到大數(shù)取余的方法以及同余定理;簡單的bfs;
#include<stdio.h> #include<queue> #include<iostream> #include<string.h> #include<algorithm> using namespace std; bool vis[5005]; int a[20]; int T,n,c,m; struct node {int step,mod;string str; }p,q; char _char(int ss) {if(ss>=0&&ss<=9) return ss+'0';if(ss>=10&&ss<=15) return ss-10+'A'; } void bfs() {queue<node> Q;for(int i=0;i<m;i++){if(a[i]==0) continue;p.mod=a[i]%n;p.str="";p.str+=_char(a[i]);p.step=1;if(p.mod==0) {cout<<p.str<<endl;return;}if(!vis[p.mod]){ vis[p.mod]=true;Q.push(p);}//標(biāo)記是否出現(xiàn)過}while(!Q.empty()){q=Q.front();Q.pop();if(q.step>=500) continue ;for(int i=0;i<m;i++){p=q;p.mod=((p.mod*c)+a[i])%n;if(vis[p.mod]) continue;vis[p.mod]=true;//標(biāo)記是否出現(xiàn)p.step++;p.str+=_char(a[i]);if(p.mod==0) {cout<<p.str<<endl; return ;}Q.push(p);}}cout<<"give me the bomb please"<<endl; } int main() {char str1[10];scanf("%d",&T);while(T--){scanf("%d%d",&n,&c);scanf("%d",&m);getchar();for(int i=0;i<m;i++){scanf("%s",str1);if(str1[0]>='0'&&str1[0]<='9') a[i]=str1[0]-'0';else a[i]=str1[0]+10-'A';}sort(a,a+m);if(n==0){printf(a[0]==0?"0\n":"give me the bomb please\n");continue;}memset(vis,false,sizeof(vis));bfs();} }總結(jié)
以上是生活随笔為你收集整理的hdu-超级密码(BFS)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JWTToken超时刷新策略
- 下一篇: 阿里高级技术专家至简: Service