每天3题,一题解题报告@yewleb
来源:互联网 发布:怎样添加网络连接 编辑:程序博客网 时间:2024/06/01 10:24
Problem Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
#include<cstdio>#include<algorithm>#include<queue>#include<cstring>using namespace std;const int N = 100000;int n,k,dist[N+10];bool vis[N+10];int bfs(int x,int y){ queue<int>q; q.push(x); while(!q.empty()) { int t=q.front(); q.pop(); if(t == y) { return dist[t]; }//wa在此,如果用我的代码无法通过相同数据的问题。 int nt=t-1; if(nt>0 && !vis[nt]) { q.push(nt); vis[nt] = 1; dist[nt]=dist[t]+1; } nt=t+1; if(nt<=N && !vis[nt]) { q.push(nt); vis[nt] = 1; dist[nt]=dist[t]+1; } nt=t*2; if(nt<=N && !vis[nt]) { q.push(nt); vis[nt] = 1; dist[nt]=dist[t]+1; } }}int main(){ while(scanf("%d%d",&n,&k)!=EOF) { memset(dist, 0, sizeof dist ); memset(vis, 0, sizeof vis ); int ans=bfs(n,k); printf("%d\n", ans); } return 0;}
0 0
- 每天3题,一题解题报告@yewleb
- POJ1258题解题报告
- 100题解题报告
- DP一题解题报告POJ1141 Brackets Sequence
- BestCoder Round #36 ($)(一、二题解题报告)
- 2013多校3部分题题解报告
- ACM类型题解题报告
- 北大acm1001题解题报告
- 第三次训练题解题报告
- NOIP第四题解题报告
- 每天思考一题
- Codeforces Round #238 (Div. 2)前3题解题报告
- leetcode 20题 32题题解报告
- pku acm 2021题解题报告
- foj 1844 题解题报告 + 源代码
- SRM482 DIV2 AB两题解题报告
- ZOJ月赛部分题解题报告
- 计算机复试上机题解题报告
- [算法导论]快速排序
- bellman-ford的一般形式(下一站:spfa)
- javaEE学习—SSH学习前奏
- Ping详细过程
- CSS鼠标手势
- 每天3题,一题解题报告@yewleb
- 常用高速公路车检器的种类及其优缺点
- JS Date()函数用法
- java编程中的几种注释
- 求旅行者旅行费用问题
- ant和解决Unable to locate tools.jar错误
- 虚基类、虚函数与纯虚函数
- 简析椭圆曲线ECC加密算法
- GUI:图形化界面编程