给定两个整数n和k

通过 n+1或n-1 或n*2 这3种操作,使得n==k

输出最少的操作次数

//广搜,a是队列,step记录步数,vis记录哪些数被搜到过
#include<cstdio>
#include<iostream>
#define M 1000010
using namespace std;
int step[M],a[M],vis[M];
int main()
{
int n,m,head=,tail=;
scanf("%d%d",&n,&m);
a[]=n;
vis[n]=;
while(head<tail)
{
++head;
int u=a[head];
if(u==m)
{
printf("%d",step[head]);
return ;
}
if(u->=&&!vis[u-])
{
a[++tail]=u-;
step[tail]=step[head]+;
vis[u-]=;
}
if(u+<&&!vis[u+])
{
a[++tail]=u+;
step[tail]=step[head]+;
vis[u+]=;
}
if(u*<&&!vis[u*])
{
a[++tail]=u*;
step[tail]=step[head]+;
vis[u*]=;
}
}
return ;
}

Catch That Cow(poj 3278)的更多相关文章

  1. Catch That Cow (POJ - 3278)(简单BFS)

    转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82693928作者:Mercury_Lc 题目链接 题解:给你x.y,x可以加1.减 ...

  2. POJ 3278 Catch That Cow(求助大佬)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 ...

  3. 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)

    Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...

  4. POJ 3278 Catch That Cow(模板——BFS)

    题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...

  5. POJ 3278 Catch That Cow(BFS 剪枝)

    题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...

  6. POJ——3278 Catch That Cow(BFS队列)

    相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...

  7. HDU 2717 Catch That Cow (深搜)

    题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...

  8. Catch That Cow(广搜)

    个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...

  9. poj-3278 catch that cow(搜索题)

    题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

随机推荐

  1. Java线程中run和start方法的区别

    http://bbs.csdn.net/topics/350206340 Thread类中run()和start()方法的区别如下:run()方法:在本线程内调用该Runnable对象的run()方法 ...

  2. Bookmarklet

    学习 http://www.ruanyifeng.com/blog/2011/06/a_guide_for_writing_bookmarklet.html

  3. ON THE EVOLUTION OF MACHINE LEARNING: FROM LINEAR MODELS TO NEURAL NETWORKS

    ON THE EVOLUTION OF MACHINE LEARNING: FROM LINEAR MODELS TO NEURAL NETWORKS We recently interviewed ...

  4. Ioc注解

    注解: 添加注解时,需要添加context的相关 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  5. ASP.NET MVC 站点设置.html 为起始页

    1.  删除 controller="XX" 2. 确保你的工程根目录下的*.htm或*.html文件名在IIS默认文档中存在 搞定

  6. win7能上网,上网图标显示红叉的解决办法

    今天笔记本开机,发现电脑可以上网,但是上网图标显示红叉,看这很不爽,在网上搜了解决版本. 其实很简单,你按照下面步骤操作即可. 1,右击小图标,打开网络和共享中心. 2,更改适配器设置 把无用的无线都 ...

  7. 破解LR时,解决loadrunner 破解错误:license security violation.Operation is not allowed

    一.由于在压力测试执行中,出现一个-10803的错误 ,为解决这个错误,重新设置的环境变量,在次执行错误,这个问题解决了,但另外一个问题出来了,LR,打开脚本编辑器老提示找不到TEMP目录,当时没有想 ...

  8. undefined与null的区别

    最近在默默的看面试题,其中有一个题目就是“undefined和null的区别”,突然意识到自己从未关注过这个问题,心中莫名有种急躁的感觉,百度一下发现阮大神的一篇文章(http://www.ruany ...

  9. python网络编程之最简单的单工通信

    tcp_server.py from socket import * server = socket(AF_INET, SOCK_STREAM) server.bind(('',12345)) ser ...

  10. HDU2546(01背包饭卡)

    电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...