POJ 3278:Catch That Cow
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
发现广度优先搜索适合什么题呢?就是那种在每个点都给你几个选择,然后问你最短路径的问题,对,就是这样,这样的题目最适合广度优先搜索。
这次的这个就是,每次Farmer有三个选择,然后求最短到达目的地的行程。简直是广搜的模板题。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int color[1000005];
int dis[1000005]; queue<int> q; int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int N,K;
cin>>N>>K; if(N==K)
{
cout<<0<<endl;
}
else
{
memset(color,0,sizeof(color));
memset(dis,0,sizeof(dis)); q.push(N);
while(!q.empty())
{
N=q.front();
q.pop();
if(N-1==K || N+1==K || 2*N==K)
{
cout<<dis[N]+1<<endl;
break;
} if(color[N-1]==0 && N-1>=0)
{
color[N-1]=1;
dis[N-1]=dis[N]+1;
q.push(N-1);
}
if(color[N+1]==0)
{
color[N+1]=1;
dis[N+1]=dis[N]+1;
q.push(N+1);
}
if(color[2*N]==0 && 2*N<=100000)
{
color[2*N]=1;
dis[2*N]=dis[N]+1;
q.push(2*N);
}
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3278:Catch That Cow的更多相关文章
- POJ 3278: Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 44613 Accepted: 13946 ...
- POJ 3278:The merchant(LCA&DP)
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6864 Accepted: 2375 Desc ...
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
随机推荐
- Timetable CodeForces - 946D (预处理+背包)
题意:n天m节课,最多可以逃k节课,每天在学校待的时间为该天上的第一节课到最后一节课持续的时间.问怎样逃课可以使这n天在学校待的时间最短,输出最短的时间. 分析: 1.预处理出每天逃j节课时在学校待的 ...
- day04-Python运维开发基础(位运算、代码块、流程控制)
# (7)位运算符: & | ^ << >> ~ var1 = 19 var2 = 15 # & 按位与 res = var1 & var2 " ...
- kubernetes 1.17.2 结合 Ceph 13.2.8 实现 静态 动态存储 并附带一个实验
关于部署和相关原理 请自行搜索 这里 给出我的操作记录和排查问题的思路 这一节对后面的学习有巨大的作用!!! [root@bs-k8s-ceph ~]# ceph -s cluster: -1a9a- ...
- springcloud--Feign(WebService客户端)
Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便. 它具有可插拔注释支持,包括Feign注解和JAX-RS注解.Feign还支持可插拔编码器和解码器.Spr ...
- 洛谷 P2426 删数
题目传送门 解题思路: 区间DP,f[i][j]表示区间i~j可获得的最大值,因为本题的所有区间是可以直接一次性把自己全删掉的,所以所有区间初始化为被一次性删除的值,然后枚举断点,跑区间DP. AC代 ...
- HDU 4819 二维线段树
13年长春现场赛的G题,赤裸裸的二维线段树,单点更新,区间查询 不过我是第一次写二维的,一开始写T了,原因是我没有好好利用行段,说白一点,还是相当于枚举行,然后对列进行线段树,那要你写二维线段树干嘛 ...
- IT日常技能:VMware网络配置
1.0 基本概念 集线器:把一流量为M的端口分为N个端口,每个端口流量为M/N 交换机:把一流量为M的端口分为N个端口,每个端口流量仍为M 路由器:相当于两块网卡,一块连接外网并负责NAT, 另一块负 ...
- 【LeetCode】 240. 搜索二维矩阵 II
题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 mat ...
- VUE中常用的一些方法
1.获取URL中的参数 export function getUrlKey(name) { return decodeURIComponent((new RegExp('[?|&]' + na ...
- 使用BP拦截POST型请求包 (9.20 第九天)
使用BP拦截POST型请求包 1.安装phpstudy并下载wordpress 文件,安装在phpstudy的www目录下 phpstudy下载地址:https://www.xp.cn/downloa ...