Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register
北京大学《ACM-ICPC竞赛训练》暑期课面向全球招生。容量有限,报名从速!

第一次写,AC 附代码,有优化空间

Posted by 40962501 at 2017-05-13 16:14:55 on Problem 3278
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator