HDU 2717 Catch That Cow<BFS+界限优化>
来源:互联网 发布:封天战神坐骑进阶数据 编辑:程序博客网 时间:2024/06/05 03:24
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16152 Accepted Submission(s): 4833
Total Submission(s): 16152 Accepted Submission(s): 4833
Problem 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?
* 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
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4HintThe 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.
Source
USACO 2007 Open Silver
Recommend
teddy | We have carefully selected several similar problems for you: 2102 1372 1240 1072 1180
BFS水题,但是如果一直报RE的话,注意一点,当我们一直遍历*2的情况的时候,可能会超出数组范围,从而导致RE
所以,设置一个新的界限:所有的pos不会大于2000000,因为数据最大才1000000,所以在这种数据下,你顶多乘以一次2,后面再怎么操作都不可能是最优值
其他的地方就是模板吧。
BFS水题,但是如果一直报RE的话,注意一点,当我们一直遍历*2的情况的时候,可能会超出数组范围,从而导致RE
所以,设置一个新的界限:所有的pos不会大于2000000,因为数据最大才1000000,所以在这种数据下,你顶多乘以一次2,后面再怎么操作都不可能是最优值
其他的地方就是模板吧。
#include <cstdio>#include <cstring>#include <cstdlib>#include<stack>#include<queue>#include<map>#include<iostream>using namespace std;int x,y;struct Node{ int pos,steps; friend bool operator < (Node a,Node b) { return a.steps>b.steps; }} node;priority_queue<Node> q;int vis[200005];void bfs(){ node.pos=x; node.steps=0; while(!q.empty()) q.pop(); q.push(node); vis[x]=1; while(!q.empty()) { Node tp,tmp=q.top(); q.pop(); if(tmp.pos==y) { printf("%d\n",tmp.steps); return ; } for(int i=0;i<3;i++){ if(i==0) tp.pos=tmp.pos+1; else if(i==1) tp.pos=tmp.pos-1; else tp.pos=2*tmp.pos; if(tp.pos>200000)//多一点这个判断,大于200000就不用入栈 continue; if(vis[tp.pos]) continue; tp.steps=tmp.steps+1; vis[tp.pos]=1; q.push(tp); } }}int main(){ while(~scanf("%d %d",&x,&y)) { for(int i=0;i<200005;i++) vis[i]=0; bfs(); } return 0;}
阅读全文
1 0
- HDU 2717 Catch That Cow<BFS+界限优化>
- hdu 2717 Catch That Cow bfs
- hdu 2717 Catch That Cow(bfs)
- HDU 2717 Catch That Cow (BFS)
- HDU 2717--Catch That Cow【BFS】
- HDU 2717:Catch That Cow 【bfs】
- hdu-2717 Catch That Cow (BFS)
- hdu 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow 水题BFS
- HDU 2717 Catch That Cow [BFS]
- HDU 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow (BFS)
- HDU 2717 Catch That Cow (BFS)
- 【HDU 2717】Catch That Cow(BFS)
- hdu 2717 Catch That Cow (BFS)
- HDU 2717 Catch That Cow【BFS】
- HDU 2717 Catch That Cow【BFS】
- 性能测试进阶二
- 10 计算字段
- 性能测试工具-----Jmeter
- proftpd.conf文件
- 华为路由器学习指南-配置DHCP中继…
- HDU 2717 Catch That Cow<BFS+界限优化>
- 强叔侃墙_NAT_nopat示例
- tomcat运行报错Failed to start component [StandardEngine[Catalina].StandardHost[localhost].
- 强叔侃墙_NAT_NAPT示例
- ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题
- 异步群英会-线下技术沙龙-参与送书!
- LLVM学习笔记(16)
- weico.cc微博链接
- 强叔侃墙_NAT_easyip示例