HDU-1226 超级密码

本文探讨了一种从最小到最大使用广度优先搜索(BFS)确保最少O(n)复杂度的方法。通过实例演示了如何利用C++实现这一算法,并在实际场景中应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实就是一道简单的搜索题。

从小往大+BFS可以保证最少O(n)的复杂度。

#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>

#define rep(i, l, r) for(int i=l; i<=r; i++)
#define down(i, l, r) for(int i=l; i>=r; i--)
#define maxn 5678
#define MAX 1<<30

using namespace std;

int n, c, m, bn[maxn][509];
char s[5];
bool b[16];
queue <int> q; 

void Init()
{
    rep(i, 0, 15) b[i] = 0;
    rep(i, 0, maxn-1) bn[i][0] = 0;
    while (!q.empty()) q.pop();
}

int main()
{
    int t; scanf("%d", &t);
    while (t--)
    {
        Init();
        scanf("%d%d%d", &n, &c, &m);
        rep(i, 1, m)
        {
            scanf("%s", s);
            if (s[0] <= '9') b[s[0]-'0'] = 1;
            else b[s[0]-'A'+10] = 1;
        }
        if (n == 0)
        {
            if (b[0]) printf("0\n"); else printf("give me the bomb please\n");
            continue;
        }
        rep(i, 1, 15) if (b[i] && !bn[i%n][0])
        {
            bn[i%n][0] = 1; bn[i%n][1] = i; q.push(i%n); 
        }
        while (!q.empty() && !bn[0][0])
        {
            int x = q.front(); q.pop(); 
            if (bn[x][0] == 500) continue;
            rep(i, 0, 15) if (b[i] && !bn[(x*c+i)%n][0])
            {
                int y = (x*c+i)%n;
                rep(j, 1, bn[x][0]) bn[y][j+1] = bn[x][j]; bn[y][1] = i; bn[y][0] = bn[x][0]+1; q.push(y); 
            }
        }
        if (bn[0][0]) 
        {
            down(i, bn[0][0], 1) if (bn[0][i] <= 9) printf("%c", bn[0][i]+'0'); else printf("%c", bn[0][i]-10+'A'); printf("\n");
        }
        else printf("give me the bomb please\n");
    }
}
View Code

转载于:https://www.cnblogs.com/NanoApe/p/4311940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值