链接: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. L1、L2损失函数、Huber损失函数

    L1范数损失函数,也被称为最小绝对值偏差(LAD),最小绝对值误差(LAE) L2范数损失函数,也被称为最小平方误差(LSE) L2损失函数 L1损失函数 不是非常的鲁棒(robust) 鲁棒 稳定解 ...

  2. jquery实现给循环的每一项加上不同的样式

    项目中需要实现这样的效果,模块中需要展示若干的商品,这些商品的分类名称需要显示不同的背景色,一共提供了三种背景色做选择, 这样的话就需要用这三种颜色做循环,一开始我的思路是做随机分配颜色,但是这样的话 ...

  3. AcWing 228. 异或 (dfs+线性基)打卡

    题目:https://www.acwing.com/problem/content/230/ 题意:有一个图,每条边有一个权值,现在求1-n的一条路径的最大异或和,一条边能经过多次,相应的也要计算那么 ...

  4. djanjo中url路由匹配规则是啥意思

    一,django路由匹配规则的本质是通过正则表达式对用户的url进行匹配. 1,r 是正则表达式中防止转义的符号,例如在python/n代表换行,加上r就不换行了. 2,$ 正则表达式中表示以什么什么 ...

  5. 漫谈C语言结构体

    相信大家对于结构体都不陌生.在此,分享出本人对C语言结构体的学习心得.如果你发现这个总结中有你以前所未掌握的,那本文也算是有点价值了.当然,水平有限,若发现不足之处恳请指出.代码文件test.c我放在 ...

  6. Linux C遇到的常见错误

    此随笔主要记录一些Linux C遇到的常见错误,便于debug问题或自己编程时,避免发生类似的错误或问题,后续会持续更新.... 1.内存泄露问题 内存泄露是由于内存没有释放导致程序耗内存一直增大,引 ...

  7. Dubbo入门到精通学习笔记(十八):使用Redis3.0集群实现Tomcat集群的Session共享

    文章目录 1.单节点访问http://192.168.1.61:8082/pay-web-boss/: 2.增加多一个消费者节点:192.168.1.62,以同样的方式部署pay-web-boss工程 ...

  8. JSP 虚拟路径设置

    编辑server.xml 在Host标签内加  :path为虚拟路径  docBase为绝对路径 <Context path="/icon"  docBase="C ...

  9. shell 读取配置文件的用法

    https://blog.csdn.net/qq_36684665/article/details/81134179 亲测有用!

  10. log4j日志记录到文件

    要写日志信息到一个文件中,必须使用org.apache.log4j.FileAppender.有以下FileAppender的配置参数: FileAppender配置: 属性 描述 immediate ...