520B Two Buttons (数学 - bfs - dfs+记忆化搜索)

B. Two Buttons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.

Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?

Input

The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .

Output

Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.

Sample test(s)
input
4 6
output
2
input
10 1
output
9

 

题意:有2种操作。(1).n乘以2  (2).n减去1 ,对于给定的n,m   问你最小的操作数 n -> m 。

思路:今天做了下这场比赛,这是其中的b题....家里网不好....半天刷不开网页...最后做了3题...没想到最后这题错了....我刚开始想也没想就dfs了..结果后来超时了,其实这题有3种做法,一种是dfs+记忆化搜索, bfs我写的双端队列爆内存了..还是加了个记录数组,最后一种做法最简单...就是数学方法,能把m除2就除2,不行就+1,最后看花了几步。

代码1:dfs+记忆化搜索

 

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int aa[200005];
int ans = 100005;
void dfs(int n,int m,int k)
{
    if( n < 1 || n >= 2*m ||(aa[n] != 0&&k > aa[n]))return ;
    aa[n] = k;
    if(n == m)
    {
        ans = min(ans,k);
        return;
    }
    dfs(n*2,m,k+1);
    dfs(n-1,m,k+1);
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    if(n>=m)
        printf("%d\n",n-m);
    else
    {
        memset(aa,0,sizeof(aa));
        dfs(n,m,0);
        printf("%d\n",ans);
    }
}
View Code

 

代码2:bfs(双端队列)

 

#include<iostream>
#include<algorithm>
#include<cmath>
#include<deque>
#include<cstring>
#include<cstdio>
using namespace std;
struct node
{
    int n,k;
    node() {}
    node(int _n,int _k)
    {
        n = _n;
        k = _k;
    }
};
int m;
int aa[200005];
int bfs(int n,int m,int k)
{
    node now,n1,n2;
    node s = node(n,k);
    deque<node> que;
    que.push_back(s);
    while(!que.empty())
    {
        now = que.front();
        que.pop_front();
        if(now.n == m)return now.k;
        if(now.k > aa[now.n])continue;
        aa[now.n] = now.k;

        if(now.n*2 <= m || (now.k+1 <= aa[now.n*2]))
        {
            n1 = node(now.n*2,now.k+1);
            aa[now.n*2] = now.k+1;
            que.push_back(n1);
        }

        if(now.n-1 >= 1 || (now.k+1 <= aa[now.n-1]))
        {
            n2 = node(now.n-1,now.k+1);
            aa[now.n-1] = now.k+1;
            que.push_back(n2);
        }
    }
}
int main()
{
    int n;
    scanf("%d%d",&n,&m);
    if(n>=m)
    {
        printf("%d\n",n-m);
    }
    else
    {
        for(int i=0;i<=2*m;i++)
            aa[i]=100005;
        aa[n]=0;
        printf("%d\n",bfs(n,m,0));
    }
}
View Code

 

代码3:(数学)

 

#include<stdio.h>
int main()
{
    int n,m,a=0;
    scanf("%d%d",&n,&m);
    while(m>n)
    {
        if(m%2==1)
        {
            m++;
            a++;
        }
        m=m/2;
        a++;
    }
    a=a+(n-m);
    printf("%d\n",a);
    return 0;
}
View Code

 

posted @ 2015-03-02 20:17  Doli  阅读(246)  评论(0)    收藏  举报