http://poj.org/problem?id=3278

大意是说牛在原地不动,他在某点去抓牛,他有两种方式可以走,第一种走一步,往前往后都可,第二种是走现在所在点的两倍的数目。只要能够刚好到达牛所在的那个点就行了。

因为题目中给了提示用广搜BFS,在三个方向上广搜就可以,这个题是借鉴了某位大神的才写出来的http://blog.csdn.net/ffq5050139/article/details/7341377

 #include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
const int MAXN=;
using namespace std;
int vis[MAXN];
int step[MAXN];
queue<int>q;
int bfs(int n,int k)
{
int head,next;
q.push(n);
vis[n]=;
step[n]=;
while(!q.empty())
{
head=q.front();
q.pop();
for(int i=;i<=;i++)
{
if(i==) next=head-;
else if(i==) next=head+;
else if(i==) next=head*;
if(next>MAXN||next<)//边界
continue;
if(!vis[next])//重点,走过的可以不用加进去了
{
q.push(next);
step[next]=step[head]+;
vis[next]=;
}
if(next == k)
return step[next];
}
}
}
int main()
{
int n,k;
scanf("%d %d",&n,&k);
if(n>=k)
printf("%d\n",n-k);
else
{
printf("%d\n",bfs(n,k));
}
return ;
}

POJ 3278Catch That Cow的更多相关文章

  1. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  2. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  3. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  4. Poj 3189 Steady Cow Assignment (多重匹配)

    题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...

  5. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  6. poj 3617 Best Cow Line

    http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...

  7. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  8. poj 3180 The Cow Prom(强联通分量)

    http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  9. <poj - 3268> Silver Cow Party 牛のpart 最短路径问题

    本题链接 : http://poj.org/problem?id=3268 题目大意:牛们要去聚会,输入N = 顶点数(牛场):M = 边(路)的数目: X = 终点 (聚会点).问题:求来回时间的最 ...

随机推荐

  1. Eclispe使用Maven添加官方库的jar包

    先到百度或google搜索maven仓库,在仓库中搜索需要的jar包,如poi.jar. 搜索到之后找到需要的jar包,找到这里

  2. visifire3.6.8 去水印方法

    visifire 很NB的一套开源图表 不多介绍 详询google 3.0以下版本可以直接继承Chart类 override 加水印的函数就可以, 3.0以上版本需要自己编译源代码 这个水印函数藏得有 ...

  3. WPF 绑定二(绑定指定的字符串)

    xaml: <Window x:Class="WpfApplication1.Window2" xmlns="http://schemas.microsoft.co ...

  4. 更改windows服务的配置文件app.config

    /// <summary> /// 获取每次处理记录数 /// </summary> /// <returns></returns> private s ...

  5. php实现input输入框失去焦点自动保存输入框的数据

    最近做一个输入框失去焦点时自动保存数据的功能,当然就是jQuery选择器选择input,blur时,ajax提交数据给php文件,php文件保存一下数据咯.主要是要注意一下中文的问题,所以中间需要转一 ...

  6. vc列表控件的初始化

    void CManageProcessDlg::InitList() {  m_ListProcess.SetExtendedStyle(m_ListProcess.GetExtendedStyle( ...

  7. Python脚本控制的WebDriver 常用操作 <十五> 处理Navigation Bar

    下面将使用WebDriver来模拟操作:选择一个Navigation bar的选项 测试用例场景 Navigation Bar可以看作是简单的类似于tab的导航栏.一般来说导航栏都是ul+li.先定位 ...

  8. Hive表分区

    必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...

  9. 命令行插入含有中文的sql文件,报错ERROR 1366 (HY000): Incorrect stringvalue:

    --以下是插入语句: insert into sms_inbox values('123456','123456', 'cd', sysdate(), '今天天 气很好', 1, sysdate(), ...

  10. How to install DIG dns tool on windows 7

    This guide explain how to install dig dns tool on windows 7 in few steps: 1. First go to http://www. ...