Two Buttons CodeForces #520B 题解[C++]

该博客介绍了CodeForces 520B题目,即通过红色按钮使数字翻倍,蓝色按钮使数字减一,从初始数n转换到目标数m的最少操作次数。博主首先尝试用动态规划解题,然后改为使用宽度优先搜索(BFS)并进行剪枝优化,以避免无效操作导致的计算量增加。提供的C++代码实现了这一解决方案。
摘要由CSDN通过智能技术生成

题目来源


http://codeforces.com/contest/520/problem/B

题目描述


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?

大意是有一台神奇的机器. 机器有一红一绿两个按钮. 机器显示屏上一开始有一个神奇的数字 n, 按一下红色数字就变成 n-1, 按一下蓝色就变成 2*n. n 一开始给定是个正数, 但要是在按按钮的过程中 n<=0, 机器就会”duang”的一下爆炸.

现在给定 n 和要求的数字 m, 要求通过次数最少的按按钮操作把 n 变成 m.

解题思路


一开始下意识地把这道题当成动态规划来做, 即

dp[n]=1+min(dp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值