第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】
链接:https://www.nowcoder.com/acm/contest/106/J
来源:牛客网
题目描述
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
And there are many different types of trees in HUST, each of which has a number represent its type. The doctors of biology in HUST find 4 different ways to change the tree’s type x into a new type y:
1. y=x+1
2. y=x-1
3. y=x+f(x)
4. y=x-f(x)
The function f(x) is defined as the number of 1 in x in binary representation. For example, f(1)=1, f(2)=1, f(3)=2, f(10)=2.
Now the doctors are given a tree of the type A. The doctors want to change its type into B. Because each step will cost a huge amount of money, you need to help them figure out the minimum steps to change the type of the tree into B.
Remember the type number should always be a natural number (0 included).
输入描述:
One line with two integers A and B, the init type and the target type.
输出描述:
You need to print a integer representing the minimum steps.
示例1
输入
5 12
输出
3
说明
The minimum steps they should take: 5->7->10->12. Thus the answer is 3.
【题意】:通过4种操作n最少几步可以达到m。
【出处】:poj 3278
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,k;
const int MAXN=1000010;
int visited[MAXN];//判重标记,visited[i]=true表示i已经拓展过
struct step
{
int x;//位置
int steps;//到达x所需的步数
step(int xx,int s):x(xx),steps(s) {}
};
queue<step>q;//队列(Open表)
int f(int x)
{
int c=0;
while(x){
if(x&1) c++;
x>>=1;
}
return c;
}
int main()
{
cin>>n>>k;
memset(visited,0,sizeof(visited));
q.push(step(n,0));
visited[n]=1;
while(!q.empty())
{
step s=q.front();
if(s.x==k)//找到目标
{
cout<<s.steps<<endl;
return 0;
}
else
{
if(s.x-1>=0 && !visited[s.x-1])
{
q.push(step(s.x-1,s.steps+1));
visited[s.x-1]=1;
}
if(s.x+1<=MAXN && !visited[s.x+1])
{
q.push(step(s.x+1,s.steps+1));
visited[s.x+1]=1;
}
if(s.x+f(s.x)<=MAXN&&!visited[s.x+f(s.x)])
{
q.push(step(s.x+f(s.x),s.steps+1));
visited[s.x+f(s.x)]=1;
}
if(s.x-f(s.x)>=0&&!visited[s.x-f(s.x)])
{
q.push(step(s.x-f(s.x),s.steps+1));
visited[s.x-f(s.x)]=1;
}
q.pop();
}
}
return 0;
}
第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】的更多相关文章
- 第十四届华中科技大学程序设计竞赛--J Various Tree
链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees
A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)
链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...
- 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】
链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...
- 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest
链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...
- 第十四届中北大学ACM程序设计竞赛 J.ZBT的游戏
问题描述 第14届中北大学程序设计竞赛来了,集训队新买了一大堆气球,气球一共有K种颜色(1<=K<=256),气球的颜色从1-K编号. ZBT童心未泯,他发明了一种摆放气球的游戏,规则如下 ...
随机推荐
- WPF and Silverlight.ComboBox 如何通过 Binding IsDropDownOpen 实现下拉菜单展开
In the WPF example the Popup and the ToggleButton (the arrow on the right) are bound with the proper ...
- iOS笔记061 - 二维码的生成和扫描
二维码 生成二维码 二维码可以存放纯文本.名片或者URL 生成二维码的步骤: 导入CoreImage框架 通过滤镜CIFilter生成二维码 1.创建过滤器 2.恢复滤镜的默认属性 3.设置内容 4. ...
- JMeter学习笔记(七) 导出文件接口测试
导出文件接口,其实跟下载文件接口的测试类似,主要就是执行接口导出文件后保存到本地. 下载文件接口测试,参考文档:https://www.cnblogs.com/xiaoyu2018/p/1017830 ...
- C编译器MinGW安装、下载及在notepad++中运行C程序
一.C编译器MinGW的下载及安装步骤 打开MinGW官网:http://www.mingw.org/ 图一 图二 图三 图四 图五 图六 系统中配置环境变量: 图七 验证是否安装成功: CMD中运行 ...
- PoolManager
我用的PoolManager版本是5.5.2的,导入的包总共有三个文件夹:Editor,Plugins,PoolManagerExampleFiles 1.Editor这个文件夹里面的东西,顾名思义, ...
- Python 第一周编程作业
一. 编程题 1. 结合turtle库使用手册,读懂下列代码,并在jupyter编译器中运行观察结果: 依次分析下代码: 第一行 通过保留字import引用了Python中用于绘制图形的turtl ...
- SqlHelper——数据库小助手
SqlHelper其实就是一个类. 早就听说过"SqlHelper"这个名词,也查过相关的资料,但还是一头雾水.当真的去实践去用它时,就会发现其实它没那么神秘. 当敲第一个窗体的时 ...
- InfluxDB数据备份和恢复方法,支持本地和远程备份
本文属于<InfluxDB系列教程>文章系列,该系列共包括以下 17 部分: InfluxDB学习之InfluxDB的基本概念 InfluxDB学习之InfluxDB的基本操作 Influ ...
- BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】
题目链接 BZOJ4031 题解 第一眼:这不裸的矩阵树定理么 第二眼:这个模\(10^9\)是什么鬼嘛QAQ 想尝试递归求行列式,发现这是\(O(n!)\)的.. 想上高斯消元,却又处理不了逆元这个 ...
- 《c程序设计语言》读书笔记-第二个字符串任意一个在第一个字符串出现的位置,未出先返回-1
#include <stdio.h> #include <string.h> #define Num 1000 int main() { int c,i,j = 0,m = 0 ...