POJ 3278: Catch That Cow
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 44613 | Accepted: 13946 |
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
题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户開始时在n位置。设农户当前在M位置,每次移动时有三种选择:1.移动到M-1。2.移动到M+1位置;3.移动到M*2的位置。问最少移动多少次能够移动到牛所在的位置。所以能够用BFS来搜索这三个状态,直到搜索到牛所在的位置。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int N = 200100;
int n, k;
struct node
{
int x, step;
};
queue<node> q;
int vist[N]; void bfs()
{
int cow, ans;
while(!q.empty())
{
node tmp = q.front();
q.pop();
cow = tmp.x;
ans = tmp.step;
if(cow == k)
{
printf("%d\n",ans);
return ;
}
if(cow >= 1 && !vist[cow - 1]) //要保证减1后有意义,所以要cow >= 1 减一的情况
{
node temp;
vist[cow - 1] = 1;
temp.x = cow - 1;
temp.step = ans + 1;
q.push(temp);
}
if(cow <= k && !vist[cow + 1]) //加1的情况
{
node temp;
vist[cow + 1] = 1;
temp.x = cow + 1;
temp.step = ans + 1;
q.push(temp);
}
if(cow <= k && !vist[cow * 2]) //乘二的情况
{
node temp;
vist[cow * 2] = 1;
temp.x = 2 * cow;
temp.step = ans + 1;
q.push(temp);
}
}
} int main()
{
while(~scanf("%d%d",&n,&k))
{
while(!q.empty()) q.pop();
memset(vist,0,sizeof(vist));
vist[n] = 1;
node t;
t.x = n, t.step = 0;
q.push(t);
bfs();
}
return 0;
}
POJ 3278: Catch That Cow的更多相关文章
- POJ 3278:Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submi ...
- 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. 向后移动一个 ...
随机推荐
- SecureCRT 的上传和下载操作
在网上找了两篇文章,分别关于ftp和ssh的上传下载,如果有好的大家可以留言分享,不胜感谢~ 因为关于ftp的比较少,就copy上面,本人并没有验证.关于ssh用sr和sz发现一条错误,而且网上也有解 ...
- STL优先队列——踩坑日记
priority_queue 可以定义STL中的优先队列,但是优先队列在应用于自己定义的类型时需要重载<运算符,或者通过仿函数来定义比较方法,在定义比较方法的过程中,比较大的坑是STL中对于参数 ...
- web.input()和web.data() 遇到特殊字符
使用web.py的时候,web.input()和web.data() 都可以接收用户从浏览器端输入的参数. web.input()方法返回一个包含从url(GET方法)或http header(POS ...
- MYSQL从入门到放弃系列:mysql基础语法
Mysql基本语法 启动MySQL net start mysql 连接与断开服务器 mysql -h 地址 -P 端口 -u 用户名 -p 密码 跳过权限验证登录MySQL mysqld --ski ...
- BZOJ 1878 [SDOI2009]HH的项链(扫描线+树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1878 [题目大意] 给出一个数列,给出m个查询,每次查询一个区间中不相同的数字个数 [ ...
- python3开发进阶-Django框架中的ORM的常用操作的补充(F查询和Q查询,事务)
阅读目录 F查询和Q查询 事务 一.F查询和Q查询 1.F查询 查询前的准备 class Product(models.Model): name = models.CharField(max_leng ...
- Problem M: 输出九九乘法表
#include<stdio.h> int main() { int n,i,j; scanf("%d",&n); n>=&&n<= ...
- Linux下判断字符串长度
方法1:使用wc -L命令 wc -L可以获取到当前行的长度,因此对于单独行的字符串可以用这个简单的方法获取,另外wc -l则是获取当前字符串内容的行数. echo 'abc' |wc -L 注意:这 ...
- 直接拿来用!最火的Android开源项目(一)
GitHub在中国的火爆程度无需多言,越来越多的开源项目迁移到GitHub平台上.更何况,基于不要重复造轮子的原则,了解当下比较流行的Android与iOS开源项目很是必要.利用这些项目,有时能够让你 ...
- centos7 ping127.0.0.1不通
ping 127.0.0.1,localhost和本地ip都不通,所有的配置也是正确的 检查下是否禁止了ping vim /proc/sys/net/ipv4/icmp_echo_ignore_all ...