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

Re:第一次理解广搜 贴个代码

Posted by guiguai at 2017-12-03 20:43:18 on Problem 3278
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:
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