POJ 3278 Catch That Cow
2014-08-29 21:07
295 查看
Catch That Cow
Time Limit: 2000msMemory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3278
64-bit integer IO format: %lld Java class name: Main
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 KOutput
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
Source
USACO 2007 Open Silver解题:搜。。。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; struct node{ int p,step; node(int x = 0,int y = 0):p(x),step(y){} }; int n,k; bool vis[100100]; queue<node>q; int bfs(){ while(!q.empty()) q.pop(); memset(vis,false,sizeof(vis)); vis = true; q.push(node(n,0)); int tmp; while(!q.empty()){ node now = q.front(); q.pop(); if(now.p == k) return now.step; tmp = now.p+1; if(tmp <= 100000 && !vis[tmp]){ vis[tmp] = true; q.push(node(tmp,now.step+1)); } tmp = now.p-1; if(tmp >= 0 && !vis[tmp]){ vis[tmp] = true; q.push(node(tmp,now.step+1)); } tmp = now.p<<1; if(tmp <= 100000 && !vis[tmp]){ vis[tmp] = true; q.push(node(tmp,now.step+1)); } } return -1; } int main() { while(~scanf("%d %d",&n,&k)) printf("%d\n",bfs()); return 0; }
View Code
相关文章推荐
- Catch That Cow(poj 3278)
- poj 3278 Catch That Cow(简单的广搜+剪枝)
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow BFS(第一题)
- Poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow (简单BFS)
- Catch That Cow(POJ 3278)
- poj3278——Catch That Cow
- poj 3278 Catch That Cow (bfs搜索)
- poj 3278 Catch That Cow
- poj 3278 bfs(Catch That Cow)
- poj 3278 Catch That Cow (广搜,简单)
- POJ 3278 / hdu 2717 Catch That Cow (广搜)
- poj 3278 Catch That Cow(bfs)
- 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