Poj 3278 Catch That Cow
2014-12-04 13:09
435 查看
1.Link:
http://poj.org/problem?id=3278
2.Content:
Catch That Cow
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
Sample Output
Hint
The 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
3.Method:
4.Code:
5.Reference:
http://poj.org/problem?id=3278
2.Content:
Catch That Cow
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 48245 | Accepted: 15114 |
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
Hint
The 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
3.Method:
4.Code:
1 #include<iostream> 2 #include<queue> 3 4 using namespace std; 5 6 //(0 ≤ N ≤ 100,000) 7 #define MAX 200002 8 int a[MAX]; 9 int main() 10 { 11 int i,j; 12 int x,y; 13 int m,n; 14 cin>>x>>y; 15 queue<int> q; 16 a[x]=1; 17 q.push(x); 18 while(!q.empty()) 19 { 20 m=q.front(); 21 if(m==y) {cout<<a[m]-1;break;} 22 else 23 { 24 n=m-1; 25 if(n>=0&&n<MAX&&a ==0) 26 { 27 a =a[m]+1; 28 q.push(n); 29 } 30 n=m+1; 31 if(n>=0&&n<MAX&&a ==0) 32 { 33 a =a[m]+1; 34 q.push(n); 35 } 36 n=m*2; 37 if(n>=0&&n<MAX&&a ==0) 38 { 39 a =a[m]+1; 40 q.push(n); 41 } 42 } 43 q.pop(); 44 } 45 //system("pause"); 46 return 0; 47 }
5.Reference:
相关文章推荐
- POJ-3278 Catch That Cow bfs
- C - Catch That Cow POJ - 3278
- POJ 3278 Catch That Cow bfs 难度:1
- POJ 3278 Catch That Cow
- 【POJ】3278-Catch That Cow 简单BFS
- 2道裸BFS(POJ 3278 Catch That Cow / POJ 2251 Dungeon Master)
- POJ 3278 Catch That Cow ——bfs
- poj 3278 Catch That Cow(bfs广搜)
- Catch That Cow POJ - 3278
- poj3278 Catch That Cow 一维广搜 bfs入门
- POJ 3278, Catch That Cow
- POJ 3278 Catch That Cow
- poj-3278-Catch That Cow【BFS】
- POJ3278-Catch That Cow
- BFS-POJ-3278-Catch That Cow
- POJ---3278-Catch That Cow(BFS/deque)
- poj 3278 Catch That Cow
- 【poj 3278】Catch That Cow 题意&题解&代码(C++)
- -----广搜 POJ 3278-Catch That Cow
- POJ 3278 Catch That Cow 【bfs+队列】