链接:https://www.nowcoder.com/acm/contest/106/J
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

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.

输入例子:
5 12
输出例子:
3

-->

示例1

输入

5 12

输出

3

说明

The minimum steps they should take: 5->7->10->12. Thus the answer is 3.

开始以为广搜会t,然而并不会
/*
data:2018.5.20
author:gsw
link:https://www.nowcoder.com/acm/contest/106#question
*/
#define ll long long
#define IO ios::sync_with_stdio(false); #include<math.h>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1000005 int brainy[maxn];
int vis[maxn];
int getbrainy(int a)
{
int ans=;
while(a>)
{
if(a&)ans++;
a=a>>;
}
return ans;
}
void init()
{
for(int i=;i<maxn;i++)
brainy[i]=getbrainy(i);
memset(vis,,sizeof(vis));
}
class Step
{
public:
int x,st;
};
void bfs(int a,int b)
{
Step be,ne;
be.x=a;be.st=;
queue<Step> q;
q.push(be);
vis[be.x]=;
while(!q.empty())
{
be=q.front();
q.pop();
if(be.x==b)
{
cout<<be.st<<endl;
return;
} if(!vis[be.x+])
{
vis[be.x+]=;
ne.x=be.x+;ne.st=be.st+;
q.push(ne);
}
if((be.x-brainy[be.x])>=&&!vis[be.x-brainy[be.x]])
{
vis[be.x-brainy[be.x]]=;
ne.x=be.x-brainy[be.x];ne.st=be.st+;
q.push(ne);
}
if((be.x-)>=&&!vis[be.x-])
{
vis[be.x-]=;
ne.x=be.x-;ne.st=be.st+;
q.push(ne);
}
if(!vis[be.x+brainy[be.x]])
{
vis[be.x+brainy[be.x]]=;
ne.x=be.x+brainy[be.x];ne.st=be.st+;
q.push(ne);
}
}
}
int main()
{
int a,b;
init();
scanf("%d%d",&a,&b);
bfs(a,b);
}

第十四届华中科技大学程序设计竞赛--J Various Tree的更多相关文章

  1. 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

    链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  2. 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】

    题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...

  3. 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees

    A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...

  4. 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...

  5. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  6. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  7. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  8. 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest

    链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...

  9. 第十四届中北大学ACM程序设计竞赛 J.ZBT的游戏

    问题描述 第14届中北大学程序设计竞赛来了,集训队新买了一大堆气球,气球一共有K种颜色(1<=K<=256),气球的颜色从1-K编号. ZBT童心未泯,他发明了一种摆放气球的游戏,规则如下 ...

随机推荐

  1. echarts绘制饼图时的一点特殊设置

    最后的效果如图,需要在中间展示的部分换行然后字体大小不同. 以下为option的设置,使用rich里面的参数来设置更多的文本样式,使用‘\n’来控制换行 let option = { color: c ...

  2. Ruby 技能图谱

    # Ruby 技能图谱 说明: 本图谱只捡重点的列举,并非包含全部.文中所列举或没有列举的资源信息都可以在[awesome-ruby](https://github.com/markets/aweso ...

  3. 微信小程序中使用阿里ICON图标

    由于微信小程序不支持ttf字体,只支持base64的问题,需要把从图库下载下来的字体文件中的ttf文件转码为base64后使用如图 需将图中箭头所指的字体文件通过 https://transfonte ...

  4. 框架-.Net:.NET框架

    ylbtech-框架-.Net:.NET框架 .NET框架(.NET Framework) 是由微软开发,一个致力于敏捷软件开发(Agile softwaredevelopment).快速应用开发(R ...

  5. hibernate.Criteria分页排序模糊查询

    org.hibernate.Criteria criteria = simpleDAO.getSession().createCriteria(Event.class); Criterion c = ...

  6. sql find_in_set在oracle下的解决方案

    比如一张表: artile (id,type,content); type:1表示文艺类,2表示小说类,3表示传记,4表示传说,等等5,6,7,8 表数据: id type content 1 3,1 ...

  7. OO七大设计原则

    一.单一职责原则(Single Responsibility Principle,SRP) 含义: 1.避免相同的职责分散到不同的类中 2.避免一个类承担太多职责 作用: 1.可以减少类之间的耦合 2 ...

  8. 中国HBase技术社区第一届Meetup资料大合集

    2018年6月6号,由中国HBase技术社区组织,阿里云主办的中国第一次HBase Meetup在北京望京阿里中心举行,来自阿里.小米.滴滴.360等公司的各位HBase的PMC.committer共 ...

  9. J. The Volcano Eruption(圆相交+并查集)

    题目链接:https://codeforces.com/gym/101915/problem/J 思路:将所有相交的圆用并查集维护看做一个整体,然后枚举每个整体的左边界和右边界,判断能不能同时覆盖整个 ...

  10. PAT甲级——A1151 LCA_in_a_BinaryTree【30】

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...