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 |
第一次写,AC 附代码,有优化空间#include<iostream> #include<memory.h> #include<queue> using namespace std; int n,m; int vis[200001]; queue<int> q; int d[200001]; //int start=0;int end=1; void bfs(int n){ q.push(n); d[n]=0; while (!q.empty()){ int l=q.front(); q.pop(); if(l==m) return ; int t=l+1; if(t>=0&&t<=200000&&vis[t]!=1){ vis[t]=1; q.push(t); d[t]=d[l]+1; } t=l-1; if(t>=0&&t<=200000&&vis[t]!=1){ vis[t]=1; q.push(t); d[t]=d[l]+1; } t=2*l; if(t>=0&&t<=200000&&vis[t]!=1){ vis[t]=1; q.push(t); d[t]=d[l]+1; } } return; } int main(){ memset(vis,0,sizeof(vis)); cin>>n>>m; bfs(n); cout<<d[m]<<endl; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator