链接: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. 探索Redis设计与实现4:Redis内部数据结构详解——ziplist

    本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...

  2. C++ pair

    C++ pair Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型,第一 ...

  3. Branch policies on Azure Repos

    https://docs.microsoft.com/en-us/azure/devops/repos/git/branch-policies-overview?view=azure-devops B ...

  4. CentOS 安装MySQL5.7 源码方式安装

    MySQL rpm方式安装:https://www.cnblogs.com/deverz/p/9560403.html 1.卸载已经安装的MySQL yum list installed mysqlr ...

  5. HTML-参考手册: URL 编码

    ylbtech-HTML-参考手册: URL 编码 1.返回顶部 1. HTML URL 编码 参考手册 URL 编码会将字符转换为可通过因特网传输的格式. URL - 统一资源定位器 Web 浏览器 ...

  6. oraToolKit Oracle安装辅助工具的使用方法

    目录 目录 otk使用方式 使用oraToolKit进行检测安装包情况 使用oraToolKit进行检测操作系统情况 最后 otk使用方式 oraToolkit的安装在RHEL6.1 安装 Oracl ...

  7. JFreeChart教程

    图表是信息的图形表示.有可用的各种工具,它可用于创建不同类型的图表. 本教程学习什么是JFreeChart?为什么需要它,并在各种方式列出一个基于Java的应用程序或独立创建不同类型的图表. JFre ...

  8. vue中nextTick的使用

    最近使用vue的vm.$nextTick的用法比较多,现在整理一下它的用法. 推荐阅读:http://www.ruanyifeng.com/blog/2014/10/event-loop.html   ...

  9. Android/IOS APP界面设计之尺寸规范

    1.尺寸以及分辨率 iPhone的界面尺寸不用多说,640*960是基本OK的,也可以是适应5S的640*1136,马上iPhone 6也快来了(随便吐槽一下网上曝的真机谍照,真是丑到离谱...),只 ...

  10. spring自学历程

    spring几大核心功能 1.IOC/DI(控制反转/依赖注入) IOC是什么?IOC完成的事情是原先程序员主动通过new实例化对象的事情,转交给spring负责 IOC最大的作用是:解耦,程序员不需 ...