日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

hdoj 1226 超級password 【隱圖BFS】

系統 1957 0

稱號: hdoj 1226 超級password?


分析:這題屬于隱式圖搜索,狀態不是非常明顯,須要自己建立。

事實上搜索說白了就是暴力。

這個題目就是,首先對給出的能夠組成的全部的數依次枚舉。長度從小到大。

比方第一組例子,由于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;
}

    


版權聲明:本文博客原創文章。博客,未經同意,不得轉載。

hdoj 1226 超級password 【隱圖BFS】


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 新闻| 增城市| 正阳县| 财经| 黄梅县| 林西县| 安泽县| 北川| 眉山市| 定远县| 兴和县| 饶平县| 铜陵市| 新晃| 阿拉善右旗| 阳信县| 西安市| 大英县| 阿图什市| 吴川市| 洛隆县| 温泉县| 庄河市| 永泰县| 闽侯县| 金沙县| 贞丰县| 灵山县| 蓝山县| 武陟县| 江油市| 海盐县| 河津市| 唐海县| 凤庆县| 三台县| 巴里| 比如县| 武乡县| 礼泉县| 秦皇岛市|