给定两个整数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. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  2. goto,void,extern,sizeof实例

    1.#include <stdio.h>void func(int n){    int* p = NULL;    if(  n < 0 )    {        goto ST ...

  3. photon mapping阶段性总结

    PM算法看了这么久,也该是到了总结的时候了.自己实现的是PPPM(Probabilistic progressive photon mapping)的一个简化形式.之所以是简化形式是由于我的光子搜集时 ...

  4. python 入门

    bool t, f = True, False print type(t) # Prints "<type 'bool'>"   字符串 hello = 'hello' ...

  5. Jquery 表单操作

    文本框,文本区域: 获取值: 1.$("#txt").attr("value"); 2. $("txt").val(); 单选按钮: 获取值 ...

  6. Nmap备忘单:从探索到漏洞利用(Part 5)

    这是备忘单的最后一部分,在这里主要讲述漏洞评估和渗透测试. 数据库审计 列出数据库名称 nmap -sV --script=mysql-databases 192.168.195.130 上图并没有显 ...

  7. 最长公共子序列 NYOJ37

    http://acm.nyist.net/JudgeOnline/problem.php?pid=37 先逆转原来的字符串,再用原来的字符串跟逆转后的字符串进行比较,求得的最长公共子序列就是回文串,也 ...

  8. 数学复习 ---- Mathematics Notes: A Programmer's Perspective ---- by Orzer ---- 我是沙茶

    今年是好没长进的一年呢..只学了些基本的方法.. 本文记号0] x:p x类型为p1] f(x) 表示一个函数2] (n_1,n_2,...) 表示多元组,特别的,(n)表示一个一元组3] x 表示一 ...

  9. Python爬虫经验

    有时候读取同一个url,服务器可能会返回不同的response,并不是爬虫程序代码的问题,而是服务器的问题,初次试验request时,最好 把response文本保存在一个txt文件当中,以便后续的比 ...

  10. How to install OpenResty

    How to install OpenResty 15 January 2014, 6:18 am   OpenResty, also called “ngx_openresty”, is a web ...