#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. 链接错误:multiple definition of 'xxx' 问题解决及其原理

    内容借鉴 于CSDN炸鸡叔 错因 截图: “multiple definition of  'head' ” “multiple definition of  'tail' ” 解决过程: 1.首先要 ...

  2. freeswitch dialplan 基础

    freeswitch dialplan 基础 一.基础概念 dialplan 拨号方案 context 拨号表(块) extension  拨号去向 action  (拨号后执行的)动作 condit ...

  3. NetCore+MySql+EF 数据库生成实体模型

    NetCore版本    2.1 1.点击“工具”->“NuGet包管理器”->“程序包管理器控制台” 分别安装以下几个包 Mysql 版本: MySql.Data.EntityFrame ...

  4. 微信小程序获取二维码参数

    var scene = decodeURIComponent(options.scene)

  5. angular中文文档的滚动条样式

    个人感觉angular中文文档的滚动条样式非常棒,于是乎就扒了下来 https://www.angular.cn/ body::-webkit-scrollbar { /* 定义了滚动条整体的样式 * ...

  6. 7.Redis主线程阻塞原因

    7.Redis主线程阻塞原因7.1 发现阻塞7.2 内在原因7.2.1 API或数据结构使用不合理7.2.2 CPU饱和7.2.3 持久化阻塞7.3 外在原因7.3.1 CPU竞争7.3.2 内存交换 ...

  7. skynet记录2:模块简介

    稍后填坑 bson.so  client.so  lpeg.so  md5.so  skynet.so  sproto.so gate.so  harbor.so  logger.so  snlua. ...

  8. textarea下高度自适应

    1,背景:textarea的高度不会随内容的增加而自适应,会出现滚动条 解决方案: 1)使用div模拟textarea  使用h5的属性    <div contenteditable=&quo ...

  9. JSP 页面跳转的实现方法

    客户端跳转 1. 使用 href 超链接标记  <a href="new.jsp">跳转</a> 2. 使用表单提交完成跳转  <form actio ...

  10. leetcode 算法分类

    来源  https://www.bilibili.com/video/av42947553 advance 二分查找 bst dp  敲黑板 重点 图 graph 查找  search 很多都和DP吻 ...