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:154115081034 at 2016-04-03 11:50:52 > #include<stdio.h> > #include<string.h> > #include<iostream> > #include<queue> > using namespace std; > > int step[100010]; > int vis[100010]; > queue <int> q; > > int BFS(int n, int k) > { > int i; > int head, next; > q.push(n); > step[n]=0; > vis[n]=1; > > while(!q.empty()) > { > head=q.front(); > q.pop(); > for(i=0; i<3; i++) > { > if(i==0) next=head-1; > else if(i==1) next=head+1; > else next=head*2; > > if(next<0 || next>100000) continue; > if(!vis[next]) > { > q.push(next); > step[next]=step[head]+1; > vis[next]=1; > } > if(next==k) return step[next]; > } > } > } > > int main() > { > int n, k; > > while(scanf("%d%d", &n, &k)!=EOF) > { > memset(step, 0, sizeof(step)); > memset(vis, 0, sizeof(vis)); > > while(!q.empty()) q.pop(); > > if(n>=k) > printf("%d\n", n-k); > else > printf("%d\n", BFS(n, k)); > } > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator