您的位置:首页 > 其它

hdu-超级密码(BFS)

2013-11-17 21:51 225 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399

学了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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: