hdoj 1226 超级密码 【隐式图BFS】

题目:hdoj 1226 超级密码 


分析:这题属于隐式图搜索,状态不是很明显,需要自己建立。

其实搜索说白了就是暴力。

这个题目就是,首先对给出的可以组成的所有的数依次枚举,长度从小到大。

比如第一组样例,因为0不能出现在首位,那么我们枚举首位为1 和 7 看看漫步满足,

满足的话枚举第二位10 11 17 以及 70 71 77 顺便保存他们取余 n 之后的值,这样就可以剪枝,搜索过的就不用重复搜索了。

要求最早出现的BFS即可,第一个搜到的就是。

注意长度不大于500

AC代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 15;
int a[20];
int flag[60000];
struct Node
{
    string s;
    int ss;
};
Node ans;
int n,c,m;
char solve(int x)
{
    if(x>=0 && x<=9)
        return x+'0';
    return x-10+'A';
}
bool BFS()
{
    memset(flag,0,sizeof(flag));
    queue<Node> q;
    Node now,next;
    for(int i=0;i<m;i++)
    {
        if(a[i]!=0)
        {
            now.s = solve(a[i]);
            now.ss = (a[i]%n);
            if(now.ss == 0)
            {
                ans = now;
                return true;
            }
            if(flag[now.ss]==0)
            {
                flag[now.ss] = 1;
                q.push(now);
            }
        }
    }
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        //cout<<now.s<<" "<<now.ss<<endl;
        if(now.ss == 0)
        {
            ans = now;
            return true;
        }
        for(int i=0;i<m;i++)
        {
            next.s= now.s+solve(a[i]);
            next.ss = (now.ss*c+a[i])%n;
           // cout<<"NEXT:"<<next.s<<" "<<next.ss<<" "<<a[i]<<endl;
            if(flag[next.ss]==0)
            {
                flag[next.ss] = 1;
                q.push(next);
            }
        }
    }
    return false;
}
int main()
{
    //freopen("Input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&c,&m);
        for(int i=0;i<m;i++)
        {
            char c[10];
            scanf("%s",c);
            if(c[0]>='A' && c[0]<='F')
                a[i] = (c[0]-'A')+10;
            else
                a[i] = c[0]-'0';
        }
        sort(a,a+m);
        if(n==0)
        {
            if(a[0]==0)
                puts("0");
            else
                puts("give me the bomb please");
            continue;
        }
        if(BFS() && ans.s.size()<=500)
            cout<<ans.s<<endl;
        else
            puts("give me the bomb please");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值