HDU 1226 超级password

跟POJ 1465 multiple 类是。仅仅只是多了2个条件,长度不能超过500。还有就是 可能不是十进制。


bfs+同余定理,就是用 mod 来判重。

G++ 15ms


每次枚举一位,然后记录下路径然后输出就能够。



此题坑点 当n==0 的时候 假设 m个数中有0 那么答案就是0 假设没有0 就是give me the bomb please

这儿坑了我好几次。


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>

#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i< n ;i++)
#define FOR0(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define acfun std::ios::sync_with_stdio(false)

#define SIZE 500+1
using namespace std;

int n,c,m;
int a[17];

struct lx
{
    int x,mod;
    int len;
    int path;
    void init(int xx,int mm,int ll,int pp)
    {
        x=xx,mod=mm,len=ll,path=pp;
    }
}q[5001];

void bfs()
{
    int h=0,e=0;
    bool vis[5001];
    CLR(vis,0);
    lx tmp;
    tmp.init(-1,0,0,-1);
    q[h++]=tmp;
    while(e<h)
    {
        tmp=q[e++];

        if(tmp.mod==0&&tmp.x!=-1)
        {
            stack<char>out;
            while(tmp.path!=-1)
            {
                char temp;
                if(tmp.x<10&&tmp.x>=0)
                    temp=tmp.x+'0';
                else
                    temp=tmp.x-10+'A';
                out.push(temp);
                tmp=q[tmp.path];
            }
            while(!out.empty())
            {
                printf("%c",out.top());
                out.pop();
            }
            printf("
");
            return;
        }

        FOR(k,0,m)
        {
            int num=tmp.mod*c+a[k];
            int mod=num%n;
            if(a[k]==0&&tmp.len==0)continue;
            if(vis[mod]||tmp.len>=500)continue;
            lx now;
            vis[mod]=1;
            now.init(a[k],mod,tmp.len+1,e-1);
            q[h++]=now;
        }
    }
    puts("give me the bomb please");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&c,&m);
        FOR(i,0,m)
        scanf("%X",&a[i]);
        sort(a,a+m);
        if(n==0)
        {
            if(a[0]==0)
            puts("0");
            else
            puts("give me the bomb please");
            continue;
        }
        bfs();
    }
}


原文地址:https://www.cnblogs.com/llguanli/p/6907982.html