hackerearth_bfs_解锁魔术师

中文题意:一个魔术大师需要通过手里的第一张牌的点数通过与别人提供的点数相乘看是否能够得到目标点数。

原文链接:https://www.hackerearth.com/zh/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/dhoom-4/description/

思路;把魔术师第一张牌看成是广度优先搜索的第一个节点置为0,未访问过的节点通通置为-1,目标节点是魔术师第二张牌。

ac代码:

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define all(a) a.begin(),a.end()
#define bitcnt(x) __builtin_popcountll(x)
#define MOD 1000000007
#define total 5000005
#define Me 1000000000001
#define NIL 0
#define MAXN 210005
#define EPS 1e-5
#define INF (1<<28)
#define pi 3.141593
int main()
{
    int owkey,reqkey;
    int n;
    cin>>owkey>>reqkey;
    cin>>n;
    int a[1005];
    int ans[100005];
    memset(ans,-1,sizeof(ans));
    for(int i=0;i<n;++i)
        cin>>a[i];
        queue<int >q;
        q.push(owkey);
        ans[owkey]=0;
        while(!q.empty())
        {
            long long val=q.front();
            q.pop();
            if(val==reqkey)break;
            for(int i=0;i<n;++i)
            {
                long long to=a[i];
                to=to*val;
                to%=100000;
                if(ans[to]==-1){
                ans[to]=ans[val]+1;
                q.push(to);
                }
            }
        }
        printf("%d\n",ans[reqkey]);
        return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值