hdu-超级密码(BFS)
2013-11-17 21:51
225 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399
学了c++感觉有了用武之地用string存储字符串,感觉很方便;
题意:给你m个数字用任意数字组合成n的最小倍数,此题用到大数取余的方法以及同余定理;简单的bfs;
学了c++感觉有了用武之地用string存储字符串,感觉很方便;
题意:给你m个数字用任意数字组合成n的最小倍数,此题用到大数取余的方法以及同余定理;简单的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);}//标记是否出现过 } 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;//标记是否出现 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(); } }
相关文章推荐
- HDU 1226 超级密码(BFS) (还需研究)
- hdu 1226 超级密码(BFS,同余剪枝)
- HDU 1226 超级密码(BFS)
- HDU 1226超级密码(数位BFS)
- BFS-hdu-1226-超级密码
- hdu 1226 超级密码bfs
- BFS-hdu-1226-超级密码
- hdu 1226 超级密码 数位bfs(我称之为
- HDU 1226 超级密码(BFS)
- HDU 3468 Treasure Hunting(BFS+网络流之最大流)
- hdu 4255 A Famous Grid--模拟--筛素数--bfs
- HDU_BFS_2822
- HDU 1548 A strange lift (简单bfs)
- HDU - 1372 Knight Moves ( 简单BFS + floodfill )
- HDU(1728+1175) 两道BFS
- hdu 3152 Obstacle Course (bfs)
- 多校第一场:hdu 4308(bfs)
- HDU_1254——推箱子,两次BFS
- HDU 1704 Rank floyd求传递闭包 || bfs
- hdu 1043 反向bfs标号+dfs查询+stl+状态压缩