Fork me on GitHub

超级密码(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).

SampleInput

3
22 10
3
7 0 1

2 10
1
1

25 16
3
A B C

SampleOutput

110
give me the bomb please
CCB

kuangbin搜索好像有个类似的题目,也是找只能由几个数构成某个数的倍数,不同的是那个题只能有01构成而且有SPJ,这道题是输入且输出字典序最小的。

做题历程=7=,最开始思路错了去从n的倍数开始搜TLE,然后正确的思路没有判断取模时0的情况RE,再然后没有把char换成intWA,=7=简直了

思路就是从给定的几个数开始搜索,然后每次加上那几个数,取模判重一下就行了。

值得注意的是因为涉及到进制,一定要注意字母和数字之间的转换。

代码:

  1 #include <iostream>
  2 #include <string>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <sstream>
  6 #include <iomanip>
  7 #include <map>
  8 #include <stack>
  9 #include <deque>
 10 #include <queue>
 11 #include <vector>
 12 #include <set>
 13 #include <list>
 14 #include <cstring>
 15 #include <cctype>
 16 #include <algorithm>
 17 #include <iterator>
 18 #include <cmath>
 19 #include <bitset>
 20 #include <ctime>
 21 #include <fstream>
 22 #include <limits.h>
 23 #include <numeric>
 24 
 25 using namespace std;
 26 
 27 #define F first
 28 #define S second
 29 #define mian main
 30 #define ture true
 31 
 32 #define MAXN 1000000+5
 33 #define MOD 1000000007
 34 #define PI (acos(-1.0))
 35 #define EPS 1e-6
 36 #define MMT(s) memset(s, 0, sizeof s)
 37 typedef unsigned long long ull;
 38 typedef long long ll;
 39 typedef double db;
 40 typedef long double ldb;
 41 typedef stringstream sstm;
 42 const int INF = 0x3f3f3f3f;
 43 
 44 map<int,int>mp,vis;
 45 int n,k,m;
 46 
 47 int Mod(string x){
 48     int res = 0;
 49     for(int i = 0; i < x.size(); i++){
 50         res *= k;
 51         if(isdigit(x[i])){  //WA那次就是这里忘记转换数字了=7=
 52             res += x[i] - '0';
 53         }
 54         else{
 55             res += x[i] - 'A' + 10;
 56         }
 57         res %= n;
 58         //cout << res << "  ";
 59     }
 60     return res;
 61 }
 62 
 63 int bfs(){
 64     queue<string>q;
 65     for(int i = 1; i <= 16; i++){
 66         if(mp[i]){
 67             string a = "";
 68             a = a + char(i>=10?char('A'+i-10):char('0'+i));  //注意转换
 69             int flag = Mod(a);
 70             if(flag == 0){
 71                 cout << a << endl;
 72                 return 0;
 73             }
 74             else{
 75                 if(vis[flag] == 0){
 76                     vis[flag]++;
 77                     q.push(a);
 78                     //cout << a << "  " << a.size() << "  " << flag << endl;
 79                 }
 80             }
 81         }
 82     }
 83     while(!q.empty()){
 84         string tp = q.front();
 85         q.pop();
 86         if(tp.size() >= 500){
 87             return -1;
 88         }
 89         for(int i = 0; i <= 16; i++){
 90             if(mp[i]){
 91                 string nxt = tp;
 92                 nxt = nxt + char(i>=10?char('A'+i-10):char('0'+i));
 93                 int flag = Mod(nxt);
 94                 if(flag == 0){
 95                     cout << nxt << endl;
 96                     return 0;
 97                 }
 98                 else{
 99                     if(vis[flag] == 0){
100                         vis[flag]++;
101                         q.push(nxt);
102                         //cout << nxt << "  " << nxt.size() << "  " << flag <<endl;
103                     }
104                 }
105             }
106         }
107     }
108     return -1;
109 
110 }
111 
112 int main(){
113     ios_base::sync_with_stdio(false);
114     cout.tie(0);
115     cin.tie(0);
116     int t;
117     cin>>t;
118     while(t--){
119         mp.clear();
120         vis.clear();
121         cin>>n>>k>>m;
122         for(int i = 0; i < m; i++){
123             char tp;
124             cin>>tp;
125             if(isdigit(tp)){
126                 mp[tp - '0']++;
127             }
128             else{
129                 mp[tp - 'A' + 10]++;
130             }
131         }
132         if(n == 0){  //特判n为0的情况
133             if(mp[0]){
134                 cout << 0 << endl;
135                 continue;
136             }
137             else{
138                 cout << "give me the bomb please" << endl;
139                 continue;
140             }
141         }
142         if(bfs() == -1){
143             cout << "give me the bomb please" << endl;
144         }
145     }
146     return 0;
147 }

 

 

posted @ 2018-07-29 12:11  Xenny  阅读(793)  评论(0编辑  收藏  举报