HDU1226 BFS

来源:互联网 发布:lol淘宝权女刘静雯 编辑:程序博客网 时间:2024/06/07 00:14

超级密码

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2200    Accepted Submission(s): 688


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

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

Input
输入数据的第一行是一个整数T(1<=T<=300),表示测试数据的数量.每组测试数据的第一行是两个整数N(0<=N&lt;=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
322 1037 0 12 101125 163A B C
 

Sample Output
110give me the bomb pleaseCCB
Hint
Hint
Huge input, scanf is recommended.
 

Author
Ignatius.L
 

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

#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <bitset>using namespace std ;#define zero {0}#define INF 0x3f3f3f3f#define EPS 1e-6typedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}#define N 1000int n, c, m;struct node{    int s[505];    int len;};int useit[100];char num[100];bool vis[5005];int mod(node a){    int temp = 0;    for (int i = 0; i < a.len; i++)    {        temp = (temp * c + a.s[i]) % n;    }    return temp;}char getnum(int x){    if (x >= 0 && x <= 9)        return '0' + x;    else if (x >= 10 && x <= 15)        return 'A' + x - 10;    else        return 'T';}int getint(char c){    if (c >= '0' && c <= '9')        return c - '0';    else if (c >= 'A' && c <= 'F')        return c - 'A' + 10;}void print(node a){    for (int i = 0; i < a.len; i++)    {        printf("%c", getnum(a.s[i]));    }    printf("\n");}int BFS(){    memset(vis, 0, sizeof(vis));    node a;    queue<node>Q;    a.len = 0;    a.s[0] = getint(useit[0]);    Q.push(a);    int r;    // printf("**********\n");    while (!Q.empty())    {        a = Q.front();        Q.pop();        for (int i = 0; i < m; i++)        {            a.s[a.len] = useit[i];            if(a.len==0&&useit[i]==0)                continue;            a.len++;            r = mod(a);            // printf("%d\n", r);            if (!r)            {                print(a);                return 1;            }            else            {                if (!vis[r] && a.len < 499)                {                    vis[r] = 1;                    Q.push(a);                }            }            a.len--;        }    }    return 0;}int main(){#ifdef DeBUGs    freopen("//home//amb//桌面//1.in", "r", stdin);#endif    int T;    scanf("%d", &T);    while (T--)    {        char str[2];        scanf("%d%d%d", &n, &c, &m);        memset(useit, -1, sizeof(useit));        for (int i = 0; i < m; i++)        {            scanf("%s", str);            useit[i] = getint(str[0]);            if (str[0] >= '0' && str[0] <= '9')                num[str[0] - '0'] = 1;            else                num[str[0] - 'A' + 10] = 1;        }        sort(useit,useit+m);        if(n==0)        {            if(useit[0]==0)                printf("0\n");            else            printf("give me the bomb please\n");            continue;        }        bool flag = BFS();        if (!flag)            printf("give me the bomb please\n");    }    return 0;}


0 0
原创粉丝点击