CF520B——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
4 6
2
10 1
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——————【广搜或找规律】的更多相关文章
- PIGS POJ - 1149网络流(最短增广路---广搜) + 建图
		
题意: 第一行输入m和n,m是猪圈的数量,n是顾客的数量,下面n行 第 i+1行表示第i个顾客 , 输入第一个数字表示有几把猪圈的钥匙,后面输入对应的猪圈,最后一个数字输入顾客想买几头猪. 建图: 设 ...
 - poj 3984:迷宫问题(广搜,入门题)
		
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
 - 双向广搜+hash+康托展开  codevs 1225 八数码难题
		
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
 - poj 3026 Borg Maze 最小生成树 + 广搜
		
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
 - HDU 4919 Exclusive or (数论 or 打表找规律)
		
Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...
 - 【ZOJ】3785 What day is that day?  ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用
		
转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...
 - 深搜(DFS)广搜(BFS)详解
		
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
 - 队列&广搜
		
搜索里有深搜,又有广搜,而广搜的基础就是队列. 队列是一种特殊的线性表,只能在一段插入,另一端输出.输出的那一端叫做队头,输入的那一端叫队尾.是一种先进先出(FIFO)的数据结构. 正经的队列: 头文 ...
 - 什么时候用深搜(dfs)什么时候用广搜(bfs)(转)
		
1.BFS是用来搜索最短径路的解是比较合适的,比如求最少步数的解,最少交换次数的解,因为BFS搜索过程中遇到的解一定是离根最近的,所以遇到一个解,一定就是最优解,此时搜索算法可以终止.这个时候不适宜使 ...
 
随机推荐
- UML  uml基础知识
			
uml基础知识 一.了解: uml是Unified Modeling Language的缩写,意思是统一建模语言或标准建模语言. UML规范用来描述建模的概念有,类(对象的).对象.关联.职责.行为. ...
 - vs 页面浏览不显示.aspx后缀名
			
转:http://www.cnblogs.com/hllive/p/6029763.html 由vs2013新建“web窗体应用程序”的网站,URL不显示扩展名.那今天就实现该功能 1.首先打开vs2 ...
 - 874. Walking Robot Simulation
			
A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of th ...
 - centos6.5 命令行配置无线上网
			
1.驱动下载地址: RTL8188无线网卡驱动下载 链接:https://pan.baidu.com/s/1ms-EbQCDxa76jPhYUPmr9Q 密码:r2vu 2.安装步骤: [root@c ...
 - [Python]json 错误xx is not JSON serializable
			
TypeError: Decimal('1457501') is not JSON serializable 在使用json的时候经常会遇到xxx is not JSON serializable, ...
 - Eclipse MarketPlace 打不开,对话框闪退
			
原文地址: https://blog.csdn.net/wonder_boy869/article/details/81031222 Eclipse的版本更新到了4.8.0(photon版),点击he ...
 - Android Fragment之间的通信(用fragment替换掉XML布局文件中的一个线性布局)
			
1.XML布局 (1)主界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
 - 使用webbench工具测试网站访问压力
			
介绍 Webbench是一个在Linux下使用的网站压测工具.它使用fork()模拟多个客户端 同时访问我们设定的URL,测试网站在压力下工作的性能, 最多可以模拟3万个并发连接去测试网站的负载能力. ...
 - iview  之  穿梭框 transfer
			
概述 双栏穿梭选择框,常用于将多个项目从一边移动到另一边. 说明 Transfer 组件主要基于以下四个 API 来使用: :data:总体数据,数组,每项为一个对象,且必须含有 key 值,组件基于 ...
 - 集合 相关   深浅copy
			
'' 集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复. {} ''' # set1 = set({1,2,3}) # set2 = {1,2,3,[2,3],{'name':' ...