链接: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/最小步数】的更多相关文章

  1. 第十四届华中科技大学程序设计竞赛--J Various Tree

    链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  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. Ubuntu设置root密码[repost]

    From: http://hi.baidu.com/busybox/item/283e7d31433db7179cc65ef3 安装完Ubuntu后在终端使用命令:su -然后输入密码,总是不正确.原 ...

  2. 剑指Offer - 九度1513 - 二进制中1的个数

    剑指Offer - 九度1513 - 二进制中1的个数2013-11-29 23:35 题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: 输入可能包含多个测试样例. ...

  3. 《Cracking the Coding Interview》——第1章:数组和字符串——题目3

    2014-03-18 01:32 题目:对于两个字符串,判断它们是否是Anagrams. 解法:统计俩单词字母构成是否相同即可. 代码: // 1.3 Given two strings, write ...

  4. maven的一些使用配置!

    1.国外库太慢,更换为国内镜像库在你的maven安装目录下找到conf目录下的setting.xml修改:<mirrors> <id>CN</id> <nam ...

  5. python学习总结---函数使用 and 装饰器

    # 函数使用 ### 零碎知识 - 灵活的if-else ```python a = 3 if False else 5 print(a) ''' if False: a = 3 else: a = ...

  6. iterm2+vim使用

    iterm2+vim 终端切换为iterm2+zsh+oh my zsh,确实好用. I term2常用快捷键记录 新建标签:cmd+t 关闭标签:cmd+w 切换标签:cmd+数字, 切换全屏:cm ...

  7. PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名

    题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...

  8. 数据分析—NaN数据处理

    目的 1.查找NaN值(定位到哪一列.在列的哪个索引位置) 2.填充NaN值(向上填充.向下填充.线性填充等) 3.过滤NaN值 构建简单的Dataframe数据结构环境 import pandas ...

  9. [部署开发环境][1 vagrant] vagrant部署开发环境--安装vagrant

    # 安装教程 # 安装vagrant 教程 # 准备 - windows操作系统 - VirtualBox---Win, - vagrant_1.9.3.msi - 镜像文件https://atlas ...

  10. Could not automatically select an Xcode project. Specify one in your Podfile like so

    需要将Podfile文件放置在根目录下,而不能放置在项目的里面. 更改路径即可