[POJ] 3278 Catch That Cow
来源:互联网 发布:3m源码 编辑:程序博客网 时间:2024/06/05 07:25
Catch That CowTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 100715 Accepted: 31496DescriptionFarmer 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?InputLine 1: Two space-separated integers: N and KOutputLine 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.Sample Input5 17Sample Output4HintThe 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.SourceUSACO 2007 Open Silver
一维的bfs,坑还是有的。
没必要剪枝,会很复杂,时间本来就很宽裕。
注意的一点是,起始点和终点为同一点,不特判的话,就会出现2(先+1再-1)这种答案。
x*2还写成了x^2(逃)
//Writer:GhostCai && His Yellow Duck#include<iostream>#include<queue>#include<cstdlib>#include<cmath>using namespace std;bool vis[200005];int sx,aimx;struct point{ int x,step;}node,r;void bfs(int x){ queue<point> Q; node.x = x; node.step = 0; Q.push(node); vis[x] =1; while(!Q.empty() ){ r=Q.front() ; Q.pop(); for(int i=1;i<=3;i++){ int nx; if(i==1 ) nx=r.x+1;//不能剪啦 if(i==2) nx=r.x-1; if(i==3) nx=r.x*2;//r.x^2! if(nx>=0&&nx<=100000&&!vis[nx]){ vis[nx]=1; node.x = nx; node.step = r.step + 1; Q.push(node); } if(nx==aimx){ cout<<node.step <<endl; exit(0); } } }}int main(){ cin>>sx>>aimx; if(sx==aimx) { cout<<0<<endl; return 0; } bfs(sx); return 0; }
阅读全文
0 0
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 catch that cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- Poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 - Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ-3278-Catch That Cow
- Http请求-使用cookie注入登录态访问
- git
- java遍历map的五种方法
- 在Linux上安装Intellij IDEA
- Python操作nosql数据库
- [POJ] 3278 Catch That Cow
- 《弃子长安》第六章 九煞夺命
- [LeetCode] String to Integer (atoi)
- 编译原理之词法分析
- C语言:假设在n进制下,等式成立,567*456=150216,n的值是多少?
- java.io中的read方法详解(尤其在数组流即在ByteArrayInputStream中)
- 三分钟学会使用log4J
- java基础知识总结
- WeaveSocket框架-Unity太空大战游戏-概述0