POJ3278
来源:互联网 发布:风量流量计软件专利 编辑:程序博客网 时间:2024/06/06 03:39
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int ans[100001],vis[100001];
queue<int> que;
int bfs(int sp,int ep)
{
que.push(sp);
int i=0,fnp,snp;
ans[sp]=0;
vis[sp]=1;
while(que.size())
{
fnp=que.front();
que.pop();
for(i=0;i<3;i++)
{
if(i==0)
snp=fnp+1;
else if(i==1)
snp=fnp-1;
else
snp=fnp*2;
if(snp<0||snp>100000)
continue;
if(vis[snp]==0)
{
que.push(snp);
ans[snp]=ans[fnp]+1;
vis[snp]=1;
}
if(snp==ep)return ans[ep];
}
}
}
int main()
{
int sp,ep;
while(cin>>sp>>ep)
{
memset(ans,1,100005);
memset(vis,0,100005);
while(!que.empty()) que.pop();
cout<<bfs(sp,ep)<<endl;
}
return 0;
}
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int ans[100001],vis[100001];
queue<int> que;
int bfs(int sp,int ep)
{
que.push(sp);
int i=0,fnp,snp;
ans[sp]=0;
vis[sp]=1;
while(que.size())
{
fnp=que.front();
que.pop();
for(i=0;i<3;i++)
{
if(i==0)
snp=fnp+1;
else if(i==1)
snp=fnp-1;
else
snp=fnp*2;
if(snp<0||snp>100000)
continue;
if(vis[snp]==0)
{
que.push(snp);
ans[snp]=ans[fnp]+1;
vis[snp]=1;
}
if(snp==ep)return ans[ep];
}
}
}
int main()
{
int sp,ep;
while(cin>>sp>>ep)
{
memset(ans,1,100005);
memset(vis,0,100005);
while(!que.empty()) que.pop();
cout<<bfs(sp,ep)<<endl;
}
return 0;
}
阅读全文
0 0
- POJ3278
- POJ3278
- POJ3278
- poj3278
- poj3278
- poj3278
- POJ3278
- POJ3278
- poj3278
- poj3278
- poj3278
- POJ3278
- poj3278
- poj3278
- poj3278
- poj3278
- POJ3278
- POJ3278
- Visual Studio 2012中使用GitHub
- ViewDragHelper详解
- C++跨平台例子
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
- 欢迎使用CSDN-markdown编辑器
- POJ3278
- Java集合-Collections类详解
- 中位数
- 十大Intellij IDEA快捷键
- LINQ简单总结:from n in ... where ... select 、Any()、All()、Take()、Skip()、 First()、 FirstOrDefault()
- Animator参考(属性动画)
- ListeningExecutorService配合CountDownLatch多线程并发处理数据
- Homebrew
- 完美的中文乱码解决方案