POJ 3278 Catch That Cow 搜索
来源:互联网 发布:linux u盘挂载位置 编辑:程序博客网 时间:2024/05/17 08:09
http://poj.org/problem?id=3278
Catch That Cow
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 44629 Accepted: 13954
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
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
Catch That Cow
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 44629 Accepted: 13954
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
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
4
#include<iostream>#include<cstring>#include<queue>using namespace std;int n,m,ans;int v[200002];struct node {int time,x;};void bfs(int T){node now,temp;queue<node> q;now.time=0;now.x=T;memset(v,0,sizeof(v));v[now.x]=1;q.push(now);while(!q.empty()){now=q.front();q.pop();if(m==now.x) {if(now.time<ans) ans=now.time;}elsefor(int i=0;i<3;i++){if(i==0) temp.x=now.x+1;if(i==1) temp.x=now.x-1;if(i==2) temp.x=2*now.x;if(temp.x>=0&&temp.x<=100000&&!v[temp.x]){temp.time=now.time+1;v[temp.x]=1;q.push(temp);}}}}int main(){while(cin>>n>>m){ans=10000;if(n>=m)cout<<n-m<<endl;else{bfs(n);cout<<ans<<endl;}}return 0;}
0 0
- poj 3278 Catch That Cow 搜索
- POJ 3278 Catch That Cow 搜索
- ♥POJ 3278-Catch That Cow【搜索】
- poj--3278Catch 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
- 财哥面京东dm的经历【帮财哥发的】
- UI-MySQL
- C语言泛型编程技术(linux,mysql广泛应用)
- C语言程序编写
- clear方法会把整个map清空吗?
- POJ 3278 Catch That Cow 搜索
- 初探STL容器之List
- Java 注释技巧
- session的生命周期
- 2014-07-09 日志
- 变长数组_相乘取结果
- Hive学习笔记-分隔符处理
- 编外话10
- C语言程序编写