POJ 3278 Catch That Cow(BFS)
题目网址:http://poj.org/problem?id=3278
题目:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 92360 | Accepted: 28999 |
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
Output
Sample Input
5 17
Sample Output
4
Hint
思路:
很简单的一道BFS题。总共只有三种操作,每次都取队首元素分别进行三种操作,再对操作结果进行判断,筛去负数和超过100,000(边界值)的情况,其他情况都入队。
代码:
1 #include <cstdio> 2 #include <queue> 3 #include <algorithm> 4 using namespace std; 5 int visited[100005]; 6 int n,k; 7 queue<int>q; 8 void bfs(){ 9 q.push(n); 10 while (!q.empty()) { 11 int x=q.front();q.pop(); 12 if(x==k){ 13 printf("%d\n",visited[x]-1);//初始次数为1,结果减去1 14 return ; 15 } 16 if(x-1>=0 && !visited[x-1]){//负数不考虑 17 q.push(x-1); 18 visited[x-1]=visited[x]+1; 19 } 20 if(x+1<=100000 && !visited[x+1]){//超过边界值的要筛去,否则会占用不必要的搜索时间 21 q.push(x+1); 22 visited[x+1]=visited[x]+1; 23 } 24 if(x*2<=100000 && !visited[x*2]){ 25 q.push(x*2); 26 visited[x*2]=visited[x]+1; 27 } 28 } 29 } 30 int main(){ 31 scanf("%d%d",&n,&k); 32 visited[n]=1;//初始位置也要赋值,防止第二次搜索到 33 bfs(); 34 return 0; 35 }
转载于:https://www.cnblogs.com/uniles/p/7145860.html
POJ 3278 Catch That Cow(BFS)相关推荐
- poj 3278 Catch That Cow (bfs)
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 1 #include< ...
- poj 3278 Catch That Cow(广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45087 Accepted: 14116 ...
- HDU 2717 Catch That Cow(BFS)
题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...
- C - Catch That Cow(BFS)
找奶牛 有三种移动方式,作为BFS搜索的方向 利用 当 n > k 时只能向后走,优化结果 #include<iostream> #include<cstring> #i ...
- HDU2717 Catch That Cow(bfs)
题意:农夫的牛跑了,给出农夫和牛在坐标轴上的位置n和k,农夫每次只能从点n移动到n-1.n+1或者n*2的位置.输出抓到牛所需要的最小移动次数. 思路:思路明显的bfs,每次搜索只按照这三种方式,标记 ...
- BFS POJ 3278 Catch That Cow
题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30924 Accepted: 9536 D ...
- POJ 3278 Catch That Cow BFS
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32071 Accepted: 9866 D ...
- bfs+dfs分析----poj 3278 Catch That Cow
题目详情 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 115430 Accepted: ...
最新文章
- 用ASP.Net(C#)连接Oracle数据库的方法
- 【 CodeForces - 799A 】Carrot Cakes(模拟,细节,有坑)
- 【英语学习】【Level 08】U05 Better option L1 Message sent
- jQuery 学习-样式篇(四):jQuery 设置和删除元素的属性
- Guava学习笔记(三):Preconditions优雅的检验参数
- 【python学习】装饰器@
- 教您简单几步实现工业树莓派正确安装RS232转USB驱动
- 服务器采集协议,H3C设备服务器采集参数认证过程(包含redfish和restfull协议)
- list对象转map
- 计算机主机放电操作,如何给主板CMOS放电的3种方法图文教程
- RuntimeError: iter() is only supported inside of tf.function or when eager execution is enabled.
- 等级保护二、三、四级内容及对比
- abupy文件结构功能
- 京东自动秒杀抢券php,【原创源码】【JavaScript】「京东超级百亿补贴」定时抢券脚本...
- MQL5-RPC来自 MQL5 的远程过程调用
- vue3项目实战---知乎日报----首页功能
- 安卓文字绘制和歌词器的简单实现
- 计算机二级c语言停考,计算机等级考试调整Fortran等语言停考
- 打造智慧幼儿园!学会一招,快速实现
- 小技巧:excel中月日年改年月日