超级密码 简单的广搜bfs(),要注意特殊情况的处理

Problem Description
Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息:
密码是一个C进制的数,并且只能由给定的M个数字构成,同时密码是一个给定十进制整数N(0<=N<=5000)的正整数倍(如果存在多个满足条件的数,那么最小的那个就是密码),如果这样的密码存在,那么当你输入它以后门将打开,如果不存在这样的密码......那就把门炸了吧.

注意:由于宝藏的历史久远,当时的系统最多只能保存500位密码.因此如果得到的密码长度大于500也不能用来开启房门,这种情况也被认为密码不存在.
 

 

Input
输入数据的第一行是一个整数T(1<=T<=300),表示测试数据的数量.每组测试数据的第一行是两个整数N(0<=N<=5000)和C(2<=C<=16),其中N表示的是题目描述中的给定十进制整数,C是密码的进制数.测试数据的第二行是一个整数M(1<=M<=16),它表示构成密码的数字的数量,然后是M个数字用来表示构成密码的数字.两个测试数据之间会有一个空行隔开.

注意:在给出的M个数字中,如果存在超过10的数,我们约定用A来表示10,B来表示11,C来表示12,D来表示13,E来表示14,F来表示15.我保证输入数据都是合法的.
 

 

Output
对于每组测试数据,如果存在要求的密码,则输出该密码,如果密码不存在,则输出"give me the bomb please".

注意:构成密码的数字不一定全部都要用上;密码有可能非常长,不要试图用一个整型变量来保存密码;我保证密码最高位不为0(除非密码本身就是0).
 

 

Sample Input
3
22 10
3
7 0 1

2 10
1
1

25 16
3
A B C
 

 

Sample Output
110
give me the bomb please
CCB
***************************************************************************************************************************
广搜(分层)
***************************************************************************************************************************
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<cstdio>
  6 #include<queue>
  7 using namespace std;
  8 int n,c,m,i,j,cas;
  9 struct node
 10 {
 11     int left,step;
 12     string str;
 13 }sr,er;
 14 queue<node>Q;
 15 bool vis[5005];
 16 int char_int(char k)
 17 {
 18     if(k<='9'&&k>='0')
 19      return k-'0';
 20     return k-'A'+10;
 21 }
 22 char int_char(int k)
 23 {
 24     if(k<=9)
 25      return k+'0';
 26     return k-10+'A';
 27 }
 28 int h[101];
 29 void bfs()
 30 {
 31     int it,jt;
 32     memset(vis,false,sizeof(vis));
 33     while(!Q.empty())
 34       Q.pop();
 35     for(it=0;it<m;it++)
 36     {
 37         if(h[it]==0)continue;
 38         sr.left=h[it]%n;
 39         sr.str="";
 40         sr.str+=int_char(h[it]);
 41         sr.step=1;
 42         if(sr.left==0)
 43         {
 44             cout<<sr.str<<endl;
 45             return;
 46         }
 47         if(!vis[sr.left])
 48         {
 49             Q.push(sr);
 50             vis[sr.left]=1;
 51         }
 52     }
 53     while(!Q.empty())
 54     {
 55         sr=Q.front();
 56         Q.pop();
 57         //cout<<"sr.left:: "<<sr.left<<endl;
 58         if(sr.step>=500)continue;
 59         for(it=0;it<m;it++)
 60         {
 61             er=sr;
 62             er.left=(er.left*c+h[it])%n;
 63             if(vis[er.left]==1)continue;
 64             er.step++;
 65             er.str+=int_char(h[it]);
 66             //cout<<er.str<<endl;
 67             if(er.left==0)
 68             {
 69                 cout<<er.str<<endl;
 70                 return;
 71             }
 72             Q.push(er);
 73             vis[er.left]=1;
 74         }
 75 
 76     }
 77     puts("give me the bomb please");
 78 }
 79 int main()
 80 {
 81     int sd[101];
 82     scanf("%d",&cas);
 83     while(cas--)
 84     {
 85         scanf("%d %d",&n,&c);
 86         scanf("%d",&m);
 87         char ch;
 88         memset(sd,0,sizeof(sd));
 89         for(i=0;i<m;i++)
 90         {
 91             cin>>ch;
 92             //cout<<"ch:: "<<ch<<endl;
 93             int p=char_int(ch);
 94             //cout<<"p:: "<<p<<endl;
 95             sd[p]=1;
 96         }
 97         m=0;
 98         for(i=0;i<16;i++)
 99          if(sd[i]==1)
100           h[m++]=i;
101         //cout<<"m:: "<<m<<endl;
102         //for(i=0;i<m;i++)
103          //cout<<"h[i]:: "<<h[i]<<endl;
104         if(n==0)
105         {
106             if(h[0]==0)
107              puts("0");
108             else
109               puts("give me the bomb please");
110             continue;
111         }
112         bfs();
113     }
114     return 0;
115 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3423358.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值