[poj3278]抓住那头牛
题目描述
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?
题目描述
每次牛都可以在数轴上移动,每次+1或者-1,或者是坐标*2,求最少的步数。
解法
非常非常非常地容易就可以知道这道题肯定是宽搜宽搜宽搜LJ附身,那么我们按照bfs一步一步来就可以了。
虽然有一些比较杂的优化,但是大体不变就可以A掉了。
ac代码
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#define N 200005
using namespace std;
struct node{int x,st;};
bool vis[N];
queue<node>q;
int main(){
int x,y;
while(~scanf("%d%d",&x,&y)){
while(!q.empty())q.pop();
memset(vis,false,sizeof(vis)); vis[x]=true;
q.push((node){x,0});
while(!q.empty()){
node u=q.front(); q.pop();
if(u.x==y){printf("%d\n",u.st);break;}
int ste=u.st+1;
if(u.x+1<=100000&&!vis[u.x+1])q.push((node){u.x+1,ste}),vis[u.x+1]=true;
if(u.x-1>=0&&!vis[u.x-1])q.push((node){u.x-1,ste}),vis[u.x-1]=true;
if(u.x*2<=100000&&!vis[u.x*2])q.push((node){u.x*2,ste}),vis[u.x*2]=true;
}
}
return 0;
}
[poj3278]抓住那头牛的更多相关文章
- POJ-3278 抓住这头牛
广搜解决. 广搜搜出最短路,直接输出返回就行了. 每个点只搜一次,而且界限进行一次判断. else 语句里面不要用if else if,这样的话就直走一条路了. #include <ios ...
- noi 2971 抓住那头牛
2971:抓住那头牛 查看 提交 统计 提问 总时间限制: 2000ms 内存限制: 65536kB 描述 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0<=N ...
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- OpenJudge 2971 抓住那头牛
总时间限制: 2000ms 内存限制: 65536kB 描述 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0< ...
- noi.openjudge——2971 抓住那头牛
http://noi.openjudge.cn/ch0205/2971/ 总时间限制: 2000ms 内存限制: 65536kB 描述 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫 ...
- OpenJudge 4001:抓住那头牛
题目链接 题解: 这个题可以用广搜来解决,从农夫到牛的走法每次都有三种选择,定义一个队列,把农夫的节点加进队列,然后以这三种走法找牛,队列先进先出,按顺序直到找到牛的位置. 代码: #include& ...
- 双向广搜 codevs 3060 抓住那头奶牛
codevs 3060 抓住那头奶牛 USACO 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...
- BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 634 Solved ...
- 2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
随机推荐
- Jquery UI 中的datepicker() ,获取日期后的回调函数onClose()
<head> //引入相关的css/js <link rel="stylesheet" href="//code.jquery.com/ui/1.10. ...
- 在 Linux 上搭建IntelliJ IDEA license server服务器
IntelliJIDEALicenseServer_linux_amd64 ,把该文件传到服务器的某个目录,我是放在了/var/local/software目录下 sudo chmod +x ./In ...
- vue 过滤器基本使用
Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化,例如时间戳格式化. 过滤器可以用在: 双花括号插值 v-bind 表达式 (2.1.0+ 开始支持). 过滤器应该被添加在 JavaSc ...
- Flutter - AAPT: error: resource android:attr/dialogCornerRadius not found.
Launching lib\main.dart on Nokia X6 in debug mode... FAILURE: Build failed with an exception. * What ...
- json模块 & pickle模块
之前学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了,所 ...
- python-小知识点-14
''' python2 python3 ''' #python2 print() print 'abc' range() xrange() 生成器 raw_input() #python3 print ...
- C. Rectangles
链接 [http://codeforces.com/group/1EzrFFyOc0/contest/1028/problem/C] 题意 给你n个矩形的左下角和右上角坐标,问你至少包含在n-1个矩形 ...
- Visual
#include int main() { std::cout<<"Hello World !"<<std::endl; char resp ...
- 【转】使用screw plus对PHP源码加密
运行环境 ubuntu 14.04 php 5.6 源码地址 https://github.com/del-xiong/screw-plus http://git.oschina.net/splot/ ...
- pandas修改全列的时间格式 无需使用apply
df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式