#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int a[],vis[];
queue<int>q; int bfs(int n,int k)
{
int head,next,i;
q.push(n);
a[n]=;
vis[n]=;
while(q.size()!=)
{
head=q.front();
q.pop();
for(i=;i<;i++)
{
if(i==)next=head-;
else if(i==)next=head+;
else if(i==)next=head*;
if(next>=&&next<=)
{
if(vis[next]==)
{
q.push(next);
a[next]=a[head]+;
vis[next]=;
}
if(next==k)
return a[next];
}
}
} }
int main()
{
int i,j,n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(a,,sizeof(a));
memset(vis,,sizeof(vis));
while(!q.empty())q.pop();
if(n>=k)
printf("%d\n",n-k);
else
{
printf("%d\n",bfs(n,k));
}
}
return ;
}

POJ - 3278 Catch That Cow bfs 线性的更多相关文章

  1. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  2. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  3. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  4. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  5. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  6. poj 3278 Catch That Cow bfs

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  7. poj 3278 Catch That Cow(bfs+队列)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  8. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

  9. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

随机推荐

  1. 微信小程序-遍历列表

    #index.js data: { "messg":"helloworld1111", "items":[ {"name" ...

  2. python 写入数据

    import sys reload(sys) sys.setdefaultencoding('utf8') import xlrd import xlwt book = xlrd.open_workb ...

  3. 计算kdj

    import pandas as pd def KDJ_K(df,n=9):    df['highest'] = df['high'].rolling(n).max()    df['lowest' ...

  4. step_by_step_xUnit_Net_ABP

    这段日子的开发一直使用Asp.net Boilerplate ,称之为项目模板自然就有开发中常用的功能,测试框架也在其中,土牛的ABP源代码都有通过测试,很遗憾的是我之前没有写过测试,不会就要去找资料 ...

  5. Python设计模式 - UML - 时序图(Sequence Diagram)

    简介 时序图表示参与者与对象之间.对象与对象之间的动态交互过程及时序关系. 时序图详细而直观地展示了对象随时间变化的状态.调用关系和消息时序,时序图中的主要元素有:参与者(Actor), 对象(Obj ...

  6. rancher2.1.7 +jenkins +harbor 自动容器CI系统(通过rancher命令行)

    jenkins脚本执行示例: //环境定义与 cd $WORKSPACEmodule=news-usercd $module/case $deploy in deploy) //发布模块 //编译/o ...

  7. 记一次git翻车事件

    昨天dmp上线了 本来整个流程是 1.在本地1.4分支开发,开发完成push到origin/1.4,在远程仓库把1.4 merge到master分支 2.本地online分支先pull一下远程onli ...

  8. jdk和tomcat的安装

    https://blog.csdn.net/angel_w/article/details/78580528

  9. oralce 的安装以及plsql的配置的html连接

    https://www.cnblogs.com/xkstudy/p/6400738.html

  10. Newtonsoft.Json 你必须知道的一些用法

    最近在做接口开发,对方团队开发了一个Web API 的接口,传输数据的格式是 JSON.当时看到这个东西,感觉很简单,也没想什么,没用多久就完成了我的功能,我完成的功能很简单,就是获取数据,然后把数据 ...