J - Two Buttons

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

Input
4 6
Output
2
Input
10 1
Output
9

Hint

In the first example you need to push the blue button once, and then push the red button once.

In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.

出错点:没加标记数组,没有看数据限制范围。

广搜:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node{ int out;
int dep;
}pos;
const int maxn=11000;
bool mark[maxn];
queue<node>Q;
int aim;
int bfs(int n){ memset(mark,0,sizeof(mark));
pos.out=n;
pos.dep=0;
Q.push(pos);
mark[pos.out]=1;
while(!Q.empty()){ node tmp=Q.front();
Q.pop();
if(tmp.out!=aim){ node tmp1,tmp2;
tmp1.dep=tmp.dep+1;
tmp1.out=tmp.out*2;
if(tmp1.out>=1&&tmp1.out<=10000&&!mark[tmp1.out]){ Q.push(tmp1);
mark[tmp1.out]=1;
}
tmp2.out=tmp.out-1;
tmp2.dep=tmp.dep+1;
if(tmp2.out>=1&&tmp2.out<=10000&&!mark[tmp2.out]){ Q.push(tmp2);
mark[tmp2.out]=1;
}
}else{ return tmp.dep;
}
}
}
int main(){ int n,m;
while(scanf("%d%d",&n,&m)!=EOF){ while(!Q.empty()){ Q.pop();
} aim=m;
int ans;
if(n==aim){ printf("0\n");
}else{ ans=bfs(n);
printf("%d\n",ans);
}
}
return 0;
}

规律:可以逆向思考。n-1在某种意义上等同于m+1,n*2等同于m/2。

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

  

CF520B——Two Buttons——————【广搜或找规律】的更多相关文章

  1. PIGS POJ - 1149网络流(最短增广路---广搜) + 建图

    题意: 第一行输入m和n,m是猪圈的数量,n是顾客的数量,下面n行 第 i+1行表示第i个顾客 , 输入第一个数字表示有几把猪圈的钥匙,后面输入对应的猪圈,最后一个数字输入顾客想买几头猪. 建图: 设 ...

  2. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  3. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

  4. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

  5. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  6. 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用

    转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html    ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 队列&广搜

    搜索里有深搜,又有广搜,而广搜的基础就是队列. 队列是一种特殊的线性表,只能在一段插入,另一端输出.输出的那一端叫做队头,输入的那一端叫队尾.是一种先进先出(FIFO)的数据结构. 正经的队列: 头文 ...

  9. 什么时候用深搜(dfs)什么时候用广搜(bfs)(转)

    1.BFS是用来搜索最短径路的解是比较合适的,比如求最少步数的解,最少交换次数的解,因为BFS搜索过程中遇到的解一定是离根最近的,所以遇到一个解,一定就是最优解,此时搜索算法可以终止.这个时候不适宜使 ...

随机推荐

  1. C#和JAVA 访问修饰符

    JAVA ----------------------------------------------- 访问修饰符        描述 ------------------------------- ...

  2. WinForm中DataGridView的使用(四) - 区分单双击事件

    虽然DataGridView单双击事件都有,但双击事件其实也会触发单击事件的处理,所以如果双击事件和单击事件的行为不同,或者双击时不想触发单击事件,或者单击事件会阻塞双击事件的处理时(比如单击后会有弹 ...

  3. js vs2013中允许js访问json文件的解决方案

    js  vs2013中允许js访问json文件的解决方案 <?xml version="1.0" encoding="utf-8"?> <!- ...

  4. oracle goldengate的两种用法

    此文已由作者赵欣授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 自从oracle收购来了goldengate这款产品并以后对它做了一系列改进后,有非常多的用户使用它做数据迁移 ...

  5. Android下拉刷新完全解析

    http://blog.csdn.net/guolin_blog/article/details/9255575 http://www.cnblogs.com/loonggg/p/3201505.ht ...

  6. BZOJ2430 chocolate

    有一个显然的想法是因为最后要花分成n*m个小块,所以每条边一定是要被切开的. 所以直接排序就可以了qwq,按照代价从大到小切一定是最优的. #include<iostream> #incl ...

  7. cmd应用

    如何用cmd命令新建和打开一个隐藏文件夹 随着电脑的广泛应用,个人电脑的私人空间越来越大,很多人喜欢把个人的一些私隐的文件存放在电脑上,私隐文件当然是不想别人看到的,为了防止别人看不见自己的文件,可以 ...

  8. struts2官方 中文教程 系列二:Hello World项目

    先贴个本帖的地址,免得其它网站被爬去了struts2入门系列二之Hello World  即 http://www.cnblogs.com/linghaoxinpian/p/6898779.html ...

  9. UIControl笔记

    UIControl继承自UIView UIControl与Target-Action模式 使用addTarget:action:forControlEvents方法来设置某一个controlEvent ...

  10. 【洛谷 5002】专心OI - 找祖先 (树上计数)

    专心OI - 找祖先 题目背景 \(Imakf\)是一个小蒟蒻,他最近刚学了\(LCA\),他在手机\(APP\)里看到一个游戏也叫做\(LCA\)就下载了下来. 题目描述 这个游戏会给出你一棵树,这 ...