poj3278--Catch That Cow(BFS+裁剪记录)
2017-12-20 17:33
337 查看
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 pointN (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 orX+ 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 andK
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.
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 48020 | Accepted: 15054 |
Farmer John has been informed of the location of a fugitive(逃亡的;难以捉摸的;短暂的) cow and wants to catch her immediately. He starts at a pointN (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 orX+ 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 andK
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.
#include <cstdio> #include <cstdlib> #include <string.h> #include <string> #include <math.h> #include <algorithm> #include <iostream> #include <queue> #include <stack> #include <map> #include <set> using namespace std; int i,j,t,n,k,cur; int visted[200010]; int bfs() { memset(visted, 0, sizeof(visted)); int step=0; queue<int> q,qq,tmp; q.push(n); visted =1; while(!q.empty()) { while(!q.empty()) { cur = q.front(); q.pop(); if(cur == k) return step; if(cur<k) { if(visted[2*cur]==0) { qq.push(2*cur); visted[2*cur] = 1; } if(visted[cur+1]==0) { qq.push(cur+1); visted[cur+1] = 1; } } if(cur>0) { if(visted[cur-1]==0) { qq.push(cur-1); visted[cur-1] = 1; } } } tmp=qq; qq=q; q=tmp; step++; } } int main() { cin>>n>>k; cout<<bfs()<<endl; return 0; }
相关文章推荐
- poj3278 Catch That Cow |bfs|队列|Java
- POJ3278 Catch That Cow BFS入门
- POJ3278 Catch That Cow(BFS)
- poj3278 Catch that cow 代码比很多人精简。。。BFS
- POJ3278 Catch That Cow 【BFS】
- 【搜索BFS】poj3278--Catch That Cow(bfs)
- POJ3278 Catch That Cow(BFS) 坑爹的RE
- poj3278 Catch That Cow (BFS算法和队列)
- poj3278 Catch That Cow bfs
- poj3278 Catch That Cow---bfs
- poj3278 Catch That Cow(bfs搜索)
- poj3278 Catch That Cow(bfs java)
- Poj3278 BFS Catch That Cow
- POJ3278 Catch That Cow(BFS入门)
- poj3278 Catch That Cow BFS搜索 TWT Tokyo Olympic 2combo-1
- 【poj3278】Catch that cow——bfs
- POJ3278 Catch That Cow —— BFS
- poj3278 Catch That Cow(简单的一维bfs)
- [bfs] poj3278 Catch that Cow
- poj3278——Catch That Cow(BFS,剪枝)