catch the cow (P3278)

来源:互联网 发布:网络直播消费心理分析 编辑:程序博客网 时间:2024/05/02 20:13

一开始就用我习惯的queue的方法进行广搜,结果就后面重复的枝条太多了。


后来用状态记录就OK


#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<sstream>#include<algorithm>#include<queue>using namespace std;struct my{int num,t;};int ans;int s,e;my go[300000];bool vist[300000];void bfs(){memset(vist,false,sizeof(vist));int head,tail;head=tail=0;vist[s]=true;go[head].num=s;go[head].t=0;my cur;while (head<=tail){my d=go[head++];if (d.num==e){ans=d.t;return;}if (d.num-1>=0 && !vist[d.num-1]){tail++;go[tail].num=d.num-1;go[tail].t=d.t+1;vist[d.num-1]=true;}if (d.num<=e && !vist[d.num+1]){tail++;go[tail].num=d.num+1;go[tail].t=d.t+1;vist[d.num+1]=true;}if (d.num<=e && !vist[d.num*2] && d.num){tail++;go[tail].num=d.num*2;go[tail].t=d.t+1;vist[d.num*2]=true;}}}int main(){int i,j,k;while (cin>>s>>e){bfs();cout<<ans<<endl;}return 0;}

Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 34076 Accepted: 10496

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (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 orX + 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 andK

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

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

Source


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 9岁儿童咳嗽鼻塞怎么办 三个月大的狗狗流鼻涕怎么办 3个月小狗干呕流鼻涕怎么办 狗狗流鼻涕怎么办有浓 小狗狗感冒了怎么办呢 狗狗一直擤鼻涕怎么办 小狗感冒流黄鼻涕怎么办 六个月宝宝鼻塞流鼻涕怎么办 小狗吃太多吐了怎么办 狗狗晕车一直吐怎么办 狗狗已经晕车了怎么办 狗狗得犬瘟怎么办 泰迪坐车吐了怎么办 小孩感冒流鼻涕带血怎么办 孩子鼻子流鼻涕有血丝怎么办 鼻子过敏流鼻涕有血丝怎么办 孕妇感冒头痛鼻涕带血怎么办 孕妇感冒鼻塞鼻涕带血怎么办 孕晚期感冒流鼻涕打喷嚏怎么办 孕晚期感冒鼻塞流鼻涕怎么办 怀孕初期鼻涕一直流怎么办 孩子一直流鼻水怎么办 9个月宝宝流鼻涕怎么办 8个月婴儿流鼻涕怎么办 3岁宝宝鼻塞咳嗽怎么办 又感冒又咳嗽了怎么办 鼻塞有一个月了怎么办 感冒一直流清水鼻涕怎么办 孩子受凉流清水鼻涕怎么办 一遇冷空气就打喷嚏流鼻涕怎么办 打喷嚏鼻塞流清鼻涕怎么办 哺乳期鼻子不通气有鼻涕怎么办 宝宝热伤风流清鼻涕怎么办 哺乳期感冒流鼻涕怎么办最有效 小孩睡觉太晚了怎么办 孩子晚上睡觉不安稳怎么办 月子里宝宝睡觉不安稳怎么办 月子宝宝睡觉不安稳怎么办 孩子老是流黄鼻涕怎么办 孕妇流黄鼻涕怎么办速效办法 孕妇感冒流黄鼻涕怎么办