标签:
bfs,用所给数字由短到长的组成密码,开始用的优先队列,测试了很多数据都没问题,但是交上去却wa,用普通队列重写了一遍才ac
#include<iostream> #include<cstring> #include<queue> #include<ctype.h> #define maxn 5000+5 using namespace std; int n,m,z,flag; int rem[30]; int visit[maxn]; struct stu { string ans; int sum; }; void bfs() { stu x,y; queue<stu>mapp; x.ans=""; x.sum=0; mapp.push(x); while(mapp.size()) { x=mapp.front(); mapp.pop(); //cout<<x.ans<<endl; if(x.ans.size()>500) continue; if(!x.sum&&x.ans.size()) { flag=1; cout<<x.ans<<endl; return; } for(int i=0;i<=16;i++) { if(rem[i]) { y.ans=x.ans; if(i>=0&&i<=9) y.ans+=(i+'0'); else y.ans+=(i-10+'A'); y.sum=(x.sum*m+i)%n; if(!visit[y.sum]&&y.ans[0]!='0') { mapp.push(y); visit[y.sum]=1; } } } } } int main() { cin.sync_with_stdio(false); int t; cin>>t; while(t--) { cin>>n>>m>>z; flag=0; memset(visit,0,sizeof(visit)); memset(rem,0,sizeof(rem)); for(int i=0;i<z;i++) { string x; cin>>x; if(isalpha(x[0])) rem[x[0]-'A'+10]=1; else rem[x[0]-'0']=1; } //for(int i=0;i<16;i++) cout<<i<<"~"<<rem[i]<<endl; if(n==0) { if(rem[0]) { cout<<"0"<<endl;continue; } else { cout<<"give me the bomb please"<<endl;continue; } } bfs(); if(!flag) cout<<"give me the bomb please"<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/46568967