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 |
Re:求解释,那位帮看下哪出错了,感觉没问题啊?In Reply To:求解释,那位帮看下哪出错了,感觉没问题啊? Posted by:18706657485 at 2011-05-12 15:22:46 > #include <iostream> > #include <stdio.h> > #include <cstring> > using namespace std; > int m,n; > int queue[210000]; > int visit[210000]; > int bfs(); > main() > { > //freopen("in3278.txt","r",stdin); > while(cin>>m>>n) > printf("%d\n",bfs()); > return 0; > } > int bfs() > { > int head=0,end=0,q=0; > memset(visit,0,sizeof(visit)); > queue[end]=m; > visit[m]=1; > end=end+1; > while(head<end) > { > int q=queue[head++]; > //cout<<q<<endl; > if(q==n) > return visit[q]-1; > else > { > //cout<<"########\n"; > if((q+1)>=0&&(q+1)<=n&&visit[q+1]==0){queue[end++]=q+1;visit[q+1]=visit[q]+1;} > if((q-1)>=0&&(q-1)<=n&&visit[q-1]==0){queue[end++]=q-1;visit[q-1]=visit[q]+1;} > if((q*2)>=0&&(q*2)<=n&&visit[2*q]==0){queue[end++]=q*2;visit[q*2]=visit[q]+1;} > //cout<<"########\n"; > } > } > return 0; > } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator