HDU 1226 超级密码(数位BFS+同余剪枝+边界值)

超级密码

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 10   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Hint

Hint
Huge input, scanf is recommended.

Author

Ignatius.L

Source

杭州电子科技大学第三届程序设计大赛

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
#define maxn 25
int n,C,m;
char c;
int vis[5001],mset[20];
struct Node
{
    string ans;
    int val,cnt;
    Node(int x=0){cnt=1;val=x;ans="";}
    bool operator<(const Node & y) const
    {
        if(y.cnt==cnt)
        return y.ans<ans;
        return y.cnt<cnt;
    }
};
priority_queue<Node> seq;

char _char(int x)
{
    if(x<=9) return '0'+x;
    return 'A'+x-10;
}

string BFS()
{
    Node tmp;

    while(!seq.empty())
    {
        Node t=seq.top();
        seq.pop();
        //cout<<"yes"<<endl;
        //cout<<t.ans<<endl;

       // if(t.val==0&&t.cnt==1) continue;//零的清况下跳过
        if(t.cnt>500) continue;
        if(t.val==0) return t.ans;

        for(int i=0;i<m;i++)
        {
            //cout<<"yes"<<endl;

            tmp.val=(t.val*C+mset[i])%n;
            if(vis[tmp.val]) continue;
            vis[tmp.val]=1;
            //if(mset<=9)
            tmp.ans=t.ans+_char(mset[i]);
            tmp.cnt=t.cnt+1;
            seq.push(tmp);
        }
    }
    return "give me the bomb please";
}

int main()
{
    ios::sync_with_stdio(false);
    Node tmp;
    string s="";

    int t;cin>>t;//scanf("%d",&t);
    //getchar();

    while(t--)
    {
        //scanf("%d%d",&n,&C);
       //getchar();
        //scanf("%d",&m);
        //getchar();
        cin>>n>>C>>m;

        if(n)
        {
        memset(vis,0,sizeof(vis));
        while(!seq.empty()) seq.pop();

        for(int i=0;i<m;i++)
        {
           cin>>c;
           //scanf("%c",&c);
           if(c>='A'&&c<='Z')
           {
               int x=(10+c-'A')%n;
               mset[i]=10+c-'A';//存储可选答案
               tmp.val=x%n;
               tmp.ans=s+c;//初始化答案字符串
               vis[x]=1;//更新vis数组
                seq.push(tmp);
           }
           else
           {
               int x=(c-'0');
               tmp.val=x%n;
              tmp.ans=s+c;
               mset[i]=x;

               if(x)
              {
                 seq.push(tmp);
                 vis[x%n]=1;
               }
           }
        }
       // cout<<"yes"<<endl;

        sort(mset,mset+m);
        cout<<BFS()<<endl;
        }
        else
        {
            bool flag=false;
            for(int i=0;i<m;i++)
            {
                cin>>c;
                if(c=='0') flag=true;
            }
            if(flag) cout<<0<<endl;
            else cout<<"give me the bomb please"<<endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值