sdutacm-Catch That Cow
来源:互联网 发布:hotel california知乎 编辑:程序博客网 时间:2024/06/08 18:35
Catch That Cow
Time Limit: 2000MSMemory Limit: 65536KB
SubmitStatistic
ProblemDescription
Farmer John has been informedof the location of a fugitive cow and wants to catch her immediately. He startsat 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 oftransportation: walking and teleporting. * Walking: FJ can move from any pointX to the points X - 1 or X + 1 in a single minute * Teleporting: FJ can movefrom any point X to the point 2 × X in a single minute. If the cow, unaware ofits pursuit, does not move at all, how long does it take for Farmer John toretrieve it?
Input
Line 1: Two space-separatedintegers: N and K
Output
Line 1: The least amount oftime, in minutes, it takes for Farmer John to catch the fugitive cow.
ExampleInput
5 17
ExampleOutput
4
Hint
poj3278有链接提示的题目请先去链接处提交程序,AC后提交到SDUTOJ中,以便查询存档。
The fastest way for Farmer John to reach the fugitive cow is to move along thefollowing path: 5-10-9-18-17, which takes 4 minutes.
Author
#include <iostream>#include<bits/stdc++.h>using namespace std;struct node{int x,y,step;}st;int n,m,flag,book[200000];int bfs(){ queue<node>g; memset(book,0,sizeof(book)); book[st.x] = 1; g.push(st); while(!g.empty()) { node now = g.front(); g.pop(); if(now.x==m) return now.step; for(int j=0;j<3;j++) { node next; if(j==0) { next.x = now.x+1; } else if(j==1) { next.x = now.x-1; } else if(j==2) { next.x = now.x*2; } next.step = now.step+1; if(next.x==m) { return next.step; } if(next.x>=0&&next.x<=200000&&!book[next.x]) { book[next.x] = 1; g.push(next); } } } return 0 ;}int main(){ while(~scanf("%d%d",&n,&m)) { st.x = n; st.step = 0; printf("%d\n",bfs()); } return 0;}/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 4msTake Memory: 944KBSubmit time: 2017-02-15 21:00:28****************************************************/
- sdutacm-Catch That Cow
- POJ3278 Catch That Cow
- Catch That Cow
- Catch That Cow
- poj3278 Catch That Cow
- 3278. Catch That Cow
- 【HDU2717】-Catch that cow
- Catch That Cow
- F - Catch That Cow
- poj3278 - Catch That Cow
- HDOJ Catch That Cow
- 2717Catch That Cow
- POJ3278 Catch That Cow
- 3278Catch That Cow
- Catch That Cow(bfs)
- Catch that cow (H2717)
- POJ3278--Catch That Cow
- Catch That Cow
- C/C++的预编译指令
- 有关opencv的学习(6)—图像的HSV色彩空间
- WARNING:inbound connection timed out (ORA-3136)
- JAVA部分重构流程,持续更新...
- UEFI安装Arch's KDE桌面全过程
- sdutacm-Catch That Cow
- TextView对setEllipsize的设置
- Android Camera2 API的ISO和对焦设置
- 《Java高并发程序设计》总结--2.Java并行程序基础
- HTTP全部报文首部字段
- ksvcreate: Process(m000) creation failed Fri Mar 03 05:35:31 2017
- Java的语句块
- spring bean的作用域
- Android APP性能调优工具 Systrace 的使用