# BFS

1：队列stl

```#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
#define MAX INT_MAX
#define FOR(i,a,b) for( int i = a;i <= b;++i)
using namespace std;
//int n,m;
int vis[100010],step[100010];
queue<int >q;
int  bfs(int n,int m)
{
q.push(n);
step[n]=0;
vis[n]=1;

while(!q.empty())
{

for(int i=0;i<3;i++)
{
if(i==0)
{
}
else if(i==1)
{
}
else
{
}
if(next<0 || next>100000) continue;

if(!vis[next])
{
q.push(next);
vis[next]=1;
}
if(next==m)
{
return step[next];
}
}
q.pop();
}
}

int main()
{
int n,m;

while(scanf("%d%d", &n, &m)!=EOF)
{
memset(step, 0, sizeof(step));
memset(vis, 0, sizeof(vis));

while(!q.empty()) q.pop();

if(n>=m)
printf("%d\n", n-m);
else
printf("%d\n", bfs(n, m));
}
}```
View Code

2：模拟duilie

```#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
#define MAX INT_MAX
#define FOR(i,a,b) for( int i = a;i <= b;++i)
using namespace std;
int b[110000];
int step[110000],q[110000];
queue<int>que;
int BFS(int n,int m)
{

while(1)
{
for(int i=1;i<=3;++i)
{
if(i==1)
{
step2=step1+1;
}
else if(i==2)
{
step2=step1-1;
}
else if(i==3)
{
step2=step1*2;
}
if(step2<0 || step2>100000) continue;
if(b[step2]==0)
{
b[step2]=1;
tail++;
q[tail]=step2;
step[step2]=step[step1]+1;
}
if(step2==m)
{
return step[step2];
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(b,0,sizeof(b));
memset(step,0,sizeof(step));
memset(q,0,sizeof(q));
//while(!que.empty()) que.pop();
if(n>=m)
{
cout<<n-m<<endl;
}
else
{
cout<<BFS(n,m)<<endl;
}
}

}```
View Code

POJ 3278

(0)
(0)