奇妙的旅行[XDU1012]
2013-08-16 18:03
225 查看
Problem 1012 - 奇妙的旅行
Time Limit: 1000MS Memory Limit: 65536KB Difficulty:
Total Submit: 396 Accepted: 116 Special Judge: No
Description
炸鸡儿非常喜欢旅行,而且喜欢在坐标轴上旅行,从起点A到终点B(0<=A,B<=100000)。他旅行的方法很特殊,喜欢用跳的,每次跳一个地方只有三种方法:
从点C跳到点C+1。
从点C跳到点C-1。
从点C跳到点2*C。
请问他从A跳到B至少需要多少步?
Input
每组数据两个整数A,B(0<=A,B<=100000)。
Output
每例输出一行,表示至少需要的步数。
Sample Input
1 100000
0 100000
Sample Output
21
22
Hint
Source
Wang Miao
不知为什么,最近特喜欢做这样的水水的bfs.
Time Limit: 1000MS Memory Limit: 65536KB Difficulty:
Total Submit: 396 Accepted: 116 Special Judge: No
Description
炸鸡儿非常喜欢旅行,而且喜欢在坐标轴上旅行,从起点A到终点B(0<=A,B<=100000)。他旅行的方法很特殊,喜欢用跳的,每次跳一个地方只有三种方法:
从点C跳到点C+1。
从点C跳到点C-1。
从点C跳到点2*C。
请问他从A跳到B至少需要多少步?
Input
每组数据两个整数A,B(0<=A,B<=100000)。
Output
每例输出一行,表示至少需要的步数。
Sample Input
1 100000
0 100000
Sample Output
21
22
Hint
Source
Wang Miao
不知为什么,最近特喜欢做这样的水水的bfs.
#include<stdio.h> #include<string.h> #include<queue> using namespace std; struct node { int val,step; }; int S,T; bool s[200010]; int bfs() { memset(s,false,sizeof(s)); queue<node> q; while (!q.empty()) q.pop(); node st; st.val=S; st.step=0; q.push(st); s[S]=true; while (!q.empty()) { node st=q.front(); q.pop(); if (st.val==T) return st.step; if (st.val+1<=T*2) if (!s[st.val+1]) { s[st.val+1]=true; node tmp=st; tmp.step++; tmp.val=st.val+1; q.push(tmp); } if (st.val-1>0) if (!s[st.val-1]) { s[st.val-1]=true; node tmp=st; tmp.step++; tmp.val=st.val-1; q.push(tmp); } if ((st.val<<1)<=T*2) if (!s[st.val<<1]) { s[st.val<<1]=true; node tmp=st; tmp.step++; tmp.val=st.val<<1; q.push(tmp); } } } int main() { while (scanf("%d%d",&S,&T)!=EOF) { if (S>T) { printf("%d\n",S-T); continue; } int ans=bfs(); printf("%d\n",ans); } return 0; }
相关文章推荐
- XDOJ1012--奇妙的旅行
- P1091环城旅行 - 数据结构(模拟题)
- NOIP2015游记——一次开心又失望的旅行
- 独木舟上的旅行
- hdu 2066 一个人的 旅行
- bzoj1570: [JSOI2008]Blue Mary的旅行
- 集训队专题(3)1006 一个人的旅行
- 【bzoj1570】[JSOI2008]Blue Mary的旅行 最大流
- 小雷郑重承诺,在2017年之前,发表自己在云计算、在线招聘、电子商务、宠物经济、互联网金融、互联网旅行等至少6个行业领域的研究报告
- 春秋旅行安卓客户端源码项目
- 【NOIP2012TG】 开车旅行 详解+代码
- HDU 2066 一个人的旅行
- 2015年蓝桥杯省赛A组C/C++:奇妙的数字
- 【BZOJ 3531】【SDOI 2014】旅行
- 南阳题目71-独木舟上的旅行
- 【SDOI2015】【BZOJ4086】旅行计划travel
- bzoj1050:[HAOI2006]旅行comf
- HDU 2066 一个人的旅行(Dijkstra)
- Geek爱旅行 - 藏在回忆里的那个人
- 原型设计、涂鸦、旅行、公益:一个都不能少