3278: Catch That Cow

 

Problem Link

#Solution

#include<bits/stdc++.h>
using namespace std;
struct point{
int x;
int tym;
};

int vis[100010];
int n,k;
#include<cstdio>
int bfs()
{memset(vis,0,sizeof vis);
point val;

val.x=n;
val.tym=0;
vis[n]=1;

queue<point>q;
point a,b,c;
q.push(val);
while(!q.empty())
{
point val=q.front();
q.pop();

if(val.x==k) return val.tym;

a.x=val.x-1;
a.tym=val.tym+1;

if(a.x >=0 && a.x<=100010&& !vis[a.x])
{
vis[a.x]=1;
q.push(a);
}
b.x=val.x+1;
b.tym=val.tym+1;
if(b.x>=0 &&b.x<=100010 &&!vis[b.x])
{
vis[b.x]=1;
q.push(b);

}
c.x=val.x*2;
c.tym=val.tym+1;
if(c.x>=0 &&c.x<=100010 &&!vis[c.x])
{
vis[c.x]=1;
q.push(c);

}

}
}
int main()
{
while(cin>>n>>k )
{
if(n>=k)
cout<<n-k<<endl;
else cout<<bfs()<<endl;

}
return 0;
}

Advertisements

About waprogramming

I am in CSE,from CUET
This entry was posted in pku, Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s