Catch that Cow
Time limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 9276 Accepted Submission (s): 2907
Problem Descriptionfarmer John had 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 are at a point K (0≤k≤100,000) on the same number Line. Farmer John has modes of transportation:walking and teleporting.
* WALKING:FJ can move from any point x to the points X-1 or X + 1 inch a single minute
* TELEPORTING:FJ can move from any point X to the point 2xX 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?
Inputline 1:two space-separated integers:n and K
Outputline 1:the least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample INPUT5 17
Sample OUTPUT4 Test Instructions: People in the n position, the cattle in the K position, cattle do not ask people the shortest number of minutes can catch cattle, there are two ways to move 1, one-minute move n-1 or N+1 2, a minute to move 2*n step;
#include <stdio.h> #include <string.h> #include <queue> #define MAX 1000100using namespace Std;int n,m; int vis[max];struct node{int X;int step;friend bool operator < (node A,node b) {return a.step>b.step;}}; int judge (int x) {if (X < 0| | X > Max| | VIS[X]) return 0;return 1;} void BFs (int n,int k) {int i,j;priority_queue<node>q;node beg,end;beg.x=n;beg.step=0;q.push (beg); vis[n]=1; while (!q.empty ()) {beg=q.top (); Q.pop (); if (beg.x==k) {printf ("%d\n", beg.step); return;} End.x=beg.x-1;if (judge (end.x)) {Vis[end.x]=1;end.step=beg.step+1;q.push (end);} End.x=beg.x+1;if (judge (end.x)) {Vis[end.x]=1;end.step=beg.step+1;q.push (end);} End.x=beg.x*2;if (judge (end.x)) {Vis[end.x]=1;end.step=beg.step+1;q.push (end);}}} int main () {int i;while (scanf ("%d%d", &n,&m)!=eof) {memset (vis,0,sizeof (Vis)); BFs (n,m);} return 0;}
Hdoj 2717 Catch that Cow "BFS"