[VJ][bfs]Catch That Cow
Catch That Cow
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?
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.
Examples
Input
5 17
Output
4
描述:
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.
正确解法:
以前的都是二维,突然一维有点不会qaq
不能超范围,n==k时要特殊判断。
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 int n, k; 9 int book[100010]; 10 struct student 11 { 12 int x, step; 13 }que[100010]; 14 bool check(int x) 15 { 16 if (x < 0 || x>100000 || book[x] == 1) 17 return 0; 18 return 1; 19 } 20 int bfs() 21 { 22 if (n == k) return 0; 23 book[n] = 1; 24 int head = 1, tail = 2; 25 que[head].x = n; 26 que[head].step = 0; 27 while (head < tail) { 28 int tx1 = que[head].x - 1; 29 if (check(tx1)) 30 { 31 que[tail].x = tx1; 32 que[tail].step = que[head].step + 1; 33 book[tx1] = 1; 34 if (tx1 == k) 35 return que[tail].step; 36 tail++; 37 } 38 int tx2 = que[head].x + 1; 39 if (check(tx2)) 40 { 41 que[tail].x = tx2; 42 que[tail].step = que[head].step + 1; 43 book[tx2] = 1; 44 if (tx2 == k) 45 return que[tail].step; 46 tail++; 47 } 48 int tx3 = que[head].x * 2; 49 if (check(tx3)) 50 { 51 que[tail].x = tx3; 52 que[tail].step = que[head].step + 1; 53 book[tx3] = 1; 54 if (tx3 == k) 55 return que[tail].step; 56 tail++; 57 } 58 head++; 59 } 60 } 61 int main() 62 { 63 cin >> n >> k; 64 cout << bfs() << endl; 65 return 0; 66 }
View Code
转载于:https://www.cnblogs.com/Kaike/p/9914385.html
[VJ][bfs]Catch That Cow相关推荐
- c语言编辑87152,POJ 3287 (基础BFS) Catch That Cow
这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE) (1) 把初始节点S0放入Open表中: (2) 如果Open表为空,则问题无解,失败 退出: (3) 把Open表的 ...
- POJ 3278 Catch That Cow BFS
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32071 Accepted: 9866 D ...
- POJ 3278 Catch That Cow(BFS)
题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Tot ...
- 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)
链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...
- POJ3278 HDU2717 Catch That Cow【BFS】
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 100475 Accepted: 31438 ...
- Catch That Cow抓住那只奶牛(BFS,广度优先搜索)
# **Catch That Cow(抓住那只奶牛)** [题意]:农场主要去追一直逃跑的牛,每次移动有三种选择,位置加一,位置减一,位置乘二,要找到最快的方法让农夫追到牛(也就是移动的次数最少,这个 ...
- BFS POJ 3278 Catch That Cow
题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...
- 初识BFS POJ-3278 Catch That Cow FZU-2285 迷宫寻宝
初识BFS POJ-3278 Catch That Cow FZU-2285 迷宫寻宝 令人窒息的创新实验课让我们写程序. POJ-3278 Catch That Cow Farmer John ha ...
- bfs+dfs分析----poj 3278 Catch That Cow
题目详情 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 115430 Accepted: ...
最新文章
- 不使用任何框架实现CNN网络
- 深入理解Java:SimpleDateFormat安全的时间格式化
- 快速的CSV文件生成器
- 快速搭建Springboot项目的两种方式!!
- SCRM电商会员管理系统+导购端+商城app+小程序电商+移动端商城+电商web端后台管理+商城前后端系统+移动端商家端+电商app+购物车+订单+商城系统+电商看板+电商后台+优惠券+积分+会员
- WebSocket connection to,Error during WebSocket handshake: Unexpected response code: 404
- 程序员如何让自己 Be Cloud Native - 配置篇 1
- 地统计学之变异函数深入理解与分析
- 微信小程序前端【订阅消息】遇到的问题及总结
- R语言 生存分析与cox模型的学习笔记
- 互联网公司招聘,我只服百度的神操作
- WTL for MFC Programmers, Part VI - Hosting ActiveX Controls
- 凯文·凯利写给年轻人的99条人生建议(99 Additional Bits of Unsolicited Advice)
- 效率工具 Snipaste、坚果云、印象笔记
- QCustomPlot系列 进行框选放大(二)
- 卡罗拉更换变速油教程
- redhatEL7下的dns服务搭建以及ddns配置
- 《网络营销》期末考试重点试题
- 如何将图片转化为base64编码格式,在css中显示
- android编程任务进度条,Android 进度条 ProgressBar - Android 入门教程