Catch that CowTime
limit:$ MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 3278
Description
Farmer 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 in a single minute
* TELEPORTING:FJ can move from any point x to the point 2x 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:
Nand
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-Farmer John to reach the fugitive cow are to move along the following PATH:5-10-9-18-17, which takes 4 Minutes.
Simple question, don't say, direct code ...
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack > #include <string> #include <map> #include <set> #include <ctime> #define EPS 1e-6#define MAX 100005#define INF 0x3f3f3f3f#define LL long long#define pii pair<int,int> #define RD (x) scanf ("%d", &x) #define Rd2 (x, y) scanf ("%d%d", &x,&y) #define RD3 (x, Y, z) scanf ("%d%d%d", &x,&y,&z)///map<int,int> Mmap;///map<int,int >::iterator it;using namespace std;struct pos{int x; int step; POS () {} pos (int x,int step) {this->x=x,this->step=step; }};bool Vis[max];int Main () {int m,n; while (~rd2 (m,n)) {memset (vis,0,sizeof (VIS)); if (m>n) {printf ("%d\n", m-n); Continue } queue<pos> que; Que.push (Pos (m,0)); while (!Que.empty ()) {Pos temp=que.front (); if (temp.x==n) {printf ("%d\n", temp.step); Break } que.pop (); if (temp.x-1>=0 &&!vis[temp.x-1]) {Que.push (Pos (temp.x-1,temp.step+1)); Vis[temp.x-1]=true; } if (temp.x+1<=max-5 &&!vis[temp.x+1]) {Que.push (Pos (temp.x+1,temp.ste p+1)); Vis[temp.x+1]=true; } if (temp.x<<1<=max-5 &&!vis[temp.x<<1]) {Que.push (Pos (temp . x*2,temp.step+1)); vis[temp.x<<1]=true; }}} return 0;}
Catch that Cow