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. TSQL--HASH JOIN

    算法: 将其中一张表的连接列做hash,然后遍历另外一张表,对遍历出的每一行到hash表中匹配查找 要求:hash表不要求表排序或有索引

  2. [js]利用闭包向post回调函数传参数

    最近在闲逛校园XX站的时候,打算搞个破坏,试试有多少人还是用初始密码登陆.比较懒,所以直接打开控制台来写. 所以问题可以描述为: 向后端不断的post数据,id从1~5000自增,后端会根据情况来返回 ...

  3. Razor TagHelper实现Markdown转HTML

    Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 用途 Markdown的语法简洁明了.学习容易,而且功能比纯文本更强,因此有很多人 ...

  4. python,使用百度api实现复制截图中的文字

    百度云文字识别技术文档: 跳转 第三方模块安装: pip install baidu-aip pip install Pillow pip install keyboard pip install p ...

  5. Redis!

    在安装redis完成时,使用windows管理redis工具时(例如RedisDesktopManager)时出现连接不上的问题时 ping一下你的ip 和 talnet ip  看看通不通. 如果不 ...

  6. coderfoces D. Gourmet choice

      D. Gourmet choice time limit per test 2 seconds memory limit per test 256 megabytes   题目链接: https: ...

  7. HTTP响应状态码参考

    HTTP响应状态码参考: 1xx:信息 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求. Switching Protocols 服务器转换协议 ...

  8. [转] Linux 安装.src.rpm源码包的方法

    方法一:以setarch-1.3-1.src.rpm 软件包为例(可以到CSDN http://download.csdn.net/source/215173#acomment下载) 假设该文件已经存 ...

  9. pandas筛选数据。

    https://jingyan.baidu.com/article/0eb457e508b6d303f0a90572.html 假如我们想要筛选D列数据中大于0的行:df[df['D']>0] ...

  10. document.documentElement和document.body区别介绍

    document.documentElement和document.body区别介绍 * 区别 body是DOM对象黎明的body子节点,即标签 docummentElement 是整个树的根节点ro ...