| ||||||||||
| 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 | |||||||||
判断是否越界时为什么都是 <= 100000,不能先走到10万以后再一下一下的减?题目没说啊比如:5 0001 10 0000 结果肯定不一样
**************************************************
#include <cstdlib>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=200009;
bool used[maxn];
/*
*
*/
bool check(int x) //都是小于10w?题目没说啊
{
if(x>=0&&x<maxn)
return true;
return false;
}
struct node
{
int step,idx;
}st,end;
int bfs()
{
memset(used,false,sizeof(used));
used[st.idx]=true;
st.step=0;
queue<node> que;
que.push(st);
while(!que.empty())
{
node u=que.front();
que.pop();
if(u.idx == end.idx)
return u.step;
node v=u;
v.step++;
v.idx=u.idx-1;
if(check(v.idx)&&!used[v.idx])
{
used[v.idx]=true;
que.push(v);
}
if(u.idx>=maxn)
continue;
v.idx=u.idx+1;
if(check(v.idx)&&!used[v.idx])
{
used[v.idx]=true;
que.push(v);
}
v.idx=u.idx*2;
if(check(v.idx)&&!used[v.idx])
{
used[v.idx]=true;
que.push(v);
}
}
return -1;
}
int main(int argc, char** argv)
{
while(cin>>st.idx>>end.idx)
{
cout<<bfs()<<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