1. 链接:https://www.nowcoder.com/acm/contest/106/J
  2. 来源:牛客网
  3. 题目描述
  4. Its universally acknowledged that therere innumerable trees in the campus of HUST.
  5. 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 trees type x into a new type y:
  6. 1.    y=x+1
  7. 2.    y=x-1
  8. 3.    y=x+f(x)
  9. 4.    y=x-f(x)
  10. 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.
  11. 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. 
  12. Remember the type number should always be a natural number (0 included).
  13. 输入描述:
  14. One line with two integers A and B, the init type and the target type.
  15. 输出描述:
  16. You need to print a integer representing the minimum steps.
  17. 示例1
  18. 输入
  19. 5 12
  20. 输出
  21. 3
  22. 说明
  23. The minimum steps they should take: 5->7->10->12. Thus the answer is 3.

【题意】:通过4种操作n最少几步可以达到m。

【出处】:poj 3278

  1. #include<iostream>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. int n,k;
  6. const int MAXN=1000010;
  7. int visited[MAXN];//判重标记,visited[i]=true表示i已经拓展过
  8. struct step
  9. {
  10. int x;//位置
  11. int steps;//到达x所需的步数
  12. step(int xx,int s):x(xx),steps(s) {}
  13. };
  14. queue<step>q;//队列(Open表)
  15. int f(int x)
  16. {
  17. int c=0;
  18. while(x){
  19. if(x&1) c++;
  20. x>>=1;
  21. }
  22. return c;
  23. }
  24. int main()
  25. {
  26. cin>>n>>k;
  27. memset(visited,0,sizeof(visited));
  28. q.push(step(n,0));
  29. visited[n]=1;
  30. while(!q.empty())
  31. {
  32. step s=q.front();
  33. if(s.x==k)//找到目标
  34. {
  35. cout<<s.steps<<endl;
  36. return 0;
  37. }
  38. else
  39. {
  40. if(s.x-1>=0 && !visited[s.x-1])
  41. {
  42. q.push(step(s.x-1,s.steps+1));
  43. visited[s.x-1]=1;
  44. }
  45. if(s.x+1<=MAXN && !visited[s.x+1])
  46. {
  47. q.push(step(s.x+1,s.steps+1));
  48. visited[s.x+1]=1;
  49. }
  50. if(s.x+f(s.x)<=MAXN&&!visited[s.x+f(s.x)])
  51. {
  52. q.push(step(s.x+f(s.x),s.steps+1));
  53. visited[s.x+f(s.x)]=1;
  54. }
  55. if(s.x-f(s.x)>=0&&!visited[s.x-f(s.x)])
  56. {
  57. q.push(step(s.x-f(s.x),s.steps+1));
  58. visited[s.x-f(s.x)]=1;
  59. }
  60. q.pop();
  61. }
  62. }
  63. return 0;
  64. }

第十四届华中科技大学程序设计竞赛 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. WPF and Silverlight.ComboBox 如何通过 Binding IsDropDownOpen 实现下拉菜单展开

    In the WPF example the Popup and the ToggleButton (the arrow on the right) are bound with the proper ...

  2. iOS笔记061 - 二维码的生成和扫描

    二维码 生成二维码 二维码可以存放纯文本.名片或者URL 生成二维码的步骤: 导入CoreImage框架 通过滤镜CIFilter生成二维码 1.创建过滤器 2.恢复滤镜的默认属性 3.设置内容 4. ...

  3. JMeter学习笔记(七) 导出文件接口测试

    导出文件接口,其实跟下载文件接口的测试类似,主要就是执行接口导出文件后保存到本地. 下载文件接口测试,参考文档:https://www.cnblogs.com/xiaoyu2018/p/1017830 ...

  4. C编译器MinGW安装、下载及在notepad++中运行C程序

    一.C编译器MinGW的下载及安装步骤 打开MinGW官网:http://www.mingw.org/ 图一 图二 图三 图四 图五 图六 系统中配置环境变量: 图七 验证是否安装成功: CMD中运行 ...

  5. PoolManager

    我用的PoolManager版本是5.5.2的,导入的包总共有三个文件夹:Editor,Plugins,PoolManagerExampleFiles 1.Editor这个文件夹里面的东西,顾名思义, ...

  6. Python 第一周编程作业

    一.  编程题 1.  结合turtle库使用手册,读懂下列代码,并在jupyter编译器中运行观察结果: 依次分析下代码: 第一行 通过保留字import引用了Python中用于绘制图形的turtl ...

  7. SqlHelper——数据库小助手

    SqlHelper其实就是一个类. 早就听说过"SqlHelper"这个名词,也查过相关的资料,但还是一头雾水.当真的去实践去用它时,就会发现其实它没那么神秘. 当敲第一个窗体的时 ...

  8. InfluxDB数据备份和恢复方法,支持本地和远程备份

    本文属于<InfluxDB系列教程>文章系列,该系列共包括以下 17 部分: InfluxDB学习之InfluxDB的基本概念 InfluxDB学习之InfluxDB的基本操作 Influ ...

  9. BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】

    题目链接 BZOJ4031 题解 第一眼:这不裸的矩阵树定理么 第二眼:这个模\(10^9\)是什么鬼嘛QAQ 想尝试递归求行列式,发现这是\(O(n!)\)的.. 想上高斯消元,却又处理不了逆元这个 ...

  10. 《c程序设计语言》读书笔记-第二个字符串任意一个在第一个字符串出现的位置,未出先返回-1

    #include <stdio.h> #include <string.h> #define Num 1000 int main() { int c,i,j = 0,m = 0 ...