hdu 5945 Fxx and game

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5945


Fxx and game

 
 Accepts: 74
 
 Submissions: 1857
 Time Limit: 3000/1500 MS (Java/Others)
 
 Memory Limit: 131072/65536 K (Java/Others)
问题描述
青年理论计算机科学家Fxx给的学生设计了一款数字游戏。

一开始你将会得到一个数\:XX,每次游戏将给定两个参数\:k,tk,t, 任意时刻你可以对你的数执行下面两个步骤之一:

1.\:X = X - i(1 <= i <= t)1.X=Xi(1<=i<=t)2.\:2.\:X\:X\:k\:k的倍数,X = X / kX=X/k。

现在Fxx想要你告诉他最少的运行步骤,使\:X\:X变成\:11
输入描述
第一行一个整数\:T(1\leq T\leq20)\:T(1T20)表示数据组数。

接下来\:T\:T行,每行三个数\:X,k,t(0\leq t\leq10^6,1\leq X,k\leq10^6)X,k,t(0t106,1X,k106)

数据保证有解。
输出描述
输出共\:T\:T行。

每行一个整数表示答案。
输入样例
2
9 2 1
11 3 3
输出样例
4
3


题目大意:略

题目解析:用bfs搜下,注意去重(进去循环节 t )

代码如下:


#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1000009
using namespace std;
const int inf = 1e9;
const int mod = 1<<30;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
struct node
{
    int x, f;
    node(int x, int f) : x(x), f(f) {}
};
int vis[N];

int bfs(int x, int k, int t)
{
    memset(vis, 0, sizeof(vis));
    vis[x] = 1;
    queue<node> q;
    q.push(node(x, 0));
    int cnt = 0;
    while(!q.empty())
    {
        int kk = q.size(), i; cnt++;
        while(kk--)
        {
            node now = q.front(); q.pop();
            if(now.x % k == 0 && !vis[now.x / k])
            {
                int tm = now.x / k;
                q.push(node(tm, 0));
                if(tm == 1) return cnt;
                vis[tm] = 1;
            }
            if(now.f) i = t;
            else i = 1;
            for(; i <= t; i++)
            {
                if(now.x <= i) break;
                if(vis[now.x - i]) continue;
                int tm = now.x - i;
                if(tm == 1) return cnt;
                vis[tm] = 1;
                q.push(node(tm, 1));
            }
        }
    }
    return -1;
}

int main()
{
    int T, t, x, k;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &x, &k, &t);
        int ans = bfs(x, k, t);
        printf("%d\n", ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值