广搜入门,poj3278
来源:互联网 发布:博思数据 igbt销量 编辑:程序博客网 时间:2024/04/30 21:59
#include<iostream>
#include<queue>
using namespace std;
const int MAXN = 1000000;
int visit[MAXN];
struct Steps{
int stepNum;
int num;
Steps(int num_,int stepNum_):num(num_),stepNum(stepNum_){};
};
int N,K;
queue<Steps> step;
int main(){
int result(0);
memset(visit,0,sizeof(visit));
cin >> N >> K;
step.push(Steps(N,0));
visit[N] = 1;
while(!step.empty())
{
Steps top = step.front();
if(top.num == K)
{
result = top.stepNum;
break;
}
if(top.num - 1 >= 0 && visit[top.num - 1] == 0)
{
step.push(Steps(top.num - 1,top.stepNum + 1));
visit[top.num - 1] = 1;
}
if(top.num + 1 <= MAXN && visit[top.num + 1] == 0)
{
step.push(Steps(top.num + 1,top.stepNum + 1));
visit[top.num + 1] = 1;;
}
if(top.num * 2 <= MAXN && visit[top.num * 2] == 0)
{
step.push(Steps(top.num * 2,top.stepNum + 1));
visit[top.num * 2] = 1;
}
step.pop();
}
cout << result << endl;
}
#include<queue>
using namespace std;
const int MAXN = 1000000;
int visit[MAXN];
struct Steps{
int stepNum;
int num;
Steps(int num_,int stepNum_):num(num_),stepNum(stepNum_){};
};
int N,K;
queue<Steps> step;
int main(){
int result(0);
memset(visit,0,sizeof(visit));
cin >> N >> K;
step.push(Steps(N,0));
visit[N] = 1;
while(!step.empty())
{
Steps top = step.front();
if(top.num == K)
{
result = top.stepNum;
break;
}
if(top.num - 1 >= 0 && visit[top.num - 1] == 0)
{
step.push(Steps(top.num - 1,top.stepNum + 1));
visit[top.num - 1] = 1;
}
if(top.num + 1 <= MAXN && visit[top.num + 1] == 0)
{
step.push(Steps(top.num + 1,top.stepNum + 1));
visit[top.num + 1] = 1;;
}
if(top.num * 2 <= MAXN && visit[top.num * 2] == 0)
{
step.push(Steps(top.num * 2,top.stepNum + 1));
visit[top.num * 2] = 1;
}
step.pop();
}
cout << result << endl;
}
0 0
- 广搜入门,poj3278
- poj3278基本广搜
- poj3278(广搜剪枝)
- poj3278广搜
- poj3278广搜
- poj3278广搜
- POJ3278 Catch That Cow(广搜BFS)
- poj3278-catch that cow(bfs广搜)
- POJ3278 Catch That Cow(广搜BFS)
- hdu1548(BFS广搜入门)
- Catch That Cow(POJ3278)(入门)
- poj 3278 简单的广搜入门
- UVA122二叉树BFS广搜入门
- POJ3278
- POJ3278
- POJ3278
- poj3278
- poj3278
- 关于finalize机制和引用、引用队列的一些结论
- POJO类以及映射配置文件
- 动态代理
- C++软件开发第三方库大全
- 用递归实现链表的创建
- 广搜入门,poj3278
- LoardRunner集合点
- Linux入门:VIM
- 人非圣贤孰能无过丶...
- 软件开发模型【仅提供链接】
- C#中分布式事务的超时处理问题
- Java动态调用方法
- 这样总行了吧丶...
- 【C++primer学习笔记】第9章 顺序容器