链接: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. Python基础教程(004)--Python的设计哲学

    前言 Python已经成为了一门流行的编程语言. 知识点 1,优雅 2,明确 3,简单 Python开发者的哲学是:用一种方法,最好是只有一种方法来做一件事. 如果面临多种选择,Python开发者都会 ...

  2. Linux下用户-组权限配置

    cat /etc/group cat /etc/ashadow 一.用户管理 1.添加用户 #useradd 用户名 useradd php100;用cat /etc/passwd查看 分析:php1 ...

  3. switch 使用使用小技巧

    for (int i=0;i<100;i++) { switch (i) { case 1 ... 10: NSLog(@"case 1 ... 10: = %d",i); ...

  4. idea 配置tomcat

    [Toc] #一.配置全局tomcat (类似eclipse中配置tomcat的路径) ##1.1 看图,打开Edit Configuratioms... ##1.2 展开Defaults,找到tom ...

  5. DCloud-Video:Html5 Video 实现方案

    ylbtech-DCloud-Video:Html5 Video 实现方案 1.返回顶部 1.1. http://ask.dcloud.net.cn/article/569 1.2. 一. Html5 ...

  6. expand gcc case variable length

    daniel@daniel-mint ~/vex $ bash gen.sh 0x10 0x1F case 10: case 11: case 12: case 13: case 14: case 1 ...

  7. MFC DLL 导出函数的定义方式

    一直在鼓捣DLL,每天的工作都是调试一个一个的DLL,往DLL里面添加自己的代码,但是对于DLL一直不太了解啊!今天一查资料,才发现自己对于DLL编写的一些基本知识也不了解.要学习,这篇文章先总结DL ...

  8. Could not autowire. No beans of 'int' type found. less... (Ctrl+F1) Checks autowiring problems in a bean class.

    package com.cxy.netty.controller; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel ...

  9. BUUCTF RE部分题目wp

    RE 1,easyre拖进ida,得到flag 2,helloworld 将文件拖入apk改之理,得到flag 3,xor拖进ida,就是简单异或,写脚本 glo=[0x66,0x0a,0x6b,0x ...

  10. CSS3中的transform转换属性

    属性 transition-property 定义对象中参与过度的属性 transition-delay 延迟 transition-duration 持续时间 transition-timing-f ...