Poj 3278 catch that cow

Source: Internet
Author: User
Catch that cowtime limit: 2000 msmemory limit: 65536 kbthis 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 pointN(0 ≤N≤ 100,000) on a number line and the cow is at a pointK(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 pointXTo the pointsX-1 orX+ 1 in a single minute * teleporting: FJ can move from any pointXTo the point 2 ×XIn 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?

Inputline 1: two space-separated integers: NAnd KOutputline 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
Sourceusaco 2007 Open silver solution: search...
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 struct node{18     int p,step;19     node(int x = 0,int y = 0):p(x),step(y){}20 };21 int n,k;22 bool vis[100100];23 queue<node>q;24 int bfs(){25     while(!q.empty()) q.pop();26     memset(vis,false,sizeof(vis));27     vis[n] = true;28     q.push(node(n,0));29     int tmp;30     while(!q.empty()){31         node now = q.front();32         q.pop();33         if(now.p == k) return now.step;34         tmp = now.p+1;35         if(tmp <= 100000 && !vis[tmp]){36             vis[tmp] = true;37             q.push(node(tmp,now.step+1));38         }39         tmp = now.p-1;40         if(tmp >= 0 && !vis[tmp]){41             vis[tmp] = true;42             q.push(node(tmp,now.step+1));43         }44         tmp = now.p<<1;45         if(tmp <= 100000 && !vis[tmp]){46             vis[tmp] = true;47             q.push(node(tmp,now.step+1));48         }49     }50     return -1;51 }52 int main() {53     while(~scanf("%d %d",&n,&k))54         printf("%d\n",bfs());55     return 0;56 }
View code

 

Poj 3278 catch that cow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.