| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求助啊,WA了N 次。。。。各种都比较了。。为啥搜不过去啊#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int visit[1000008],step[1000008];
int BFS(int p,int k){
queue<int> all;
int now,next;
now=p;
all.push(now);
step[p]=0;
while(!all.empty()){
now=all.front();
all.pop();
next=now*2;
if(next<200003)
{ if(!visit[next])
{
step[next]=step[now]+1;
visit[next]=1;
all.push(next);
if(next==k)
{
return step[next];
}
}
}
next=now-1;
if(next>=0&&next<200003){
if(!visit[next])
{
step[next]=step[now]+1;
visit[next]=1;
all.push(next);
if(next==k)
{
// cout<<m<<endl;
return step[next];
}
}
}
next=now+1;
if(next<200003){
if(!visit[next])
{
step[next]=step[now]+1;
visit[next]=1;
all.push(next);
if(next==k)
{
// cout<<m<<endl;
return step[next];
}
}
}
}
return 0;
}
int main(){
int len;
int p,k;
while(cin>>p>>k){
memset(visit,0,sizeof(visit));
memset(step,0,sizeof(step));
if(p>=k)len=p-k;
else
len=BFS(p,k);
cout<<len<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator