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. PCB常用度量衡单位

    1英尺=12英寸 1英寸inch=1000密尔mil 1mil=25.4um 1mil=1000uin (mil密耳有时也成英丝) 1um=40uin(有些公司称微英寸为麦,其实是微英寸) 1OZ=2 ...

  2. JAVA多线程学习3--线程一些方法

    一.通过sleep方法睡眠 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行).该线程不丢失任何监视器的所属权. 二.线程优先级 线程具有优先级,范围为1-10. MAX_PRIORITY线程可以具 ...

  3. jdbc连接数据库使用sid和service_name的区别

    问题描述: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connect ...

  4. 重绘panel控件,实现panel的阴影效果

    最近想在项目中添加一个要有阴影的panel控件,找了好多资料,最后通过采用图片的方式实现了panel的阴影效果,效果图如下: 重绘代码如下: using System; using System.Co ...

  5. adbd cannot run as root in production builds

    首先必须保证手机已经root过,可以通过以下验证: $ adb shell root@dior:/ $ su root@dior:/ # 1 2 3 执行命令后,$ 变为 # 即 root 成功 但是 ...

  6. openwrt无线中继教程

    1.设置自己路由lan口的IP地址,网段不能跟上级路由的一样. 2.在无线标签下点击"扫描网络". 3.在新出现的界面中,会列出你附近的无线网络.点击你需要中继的网络右边的&quo ...

  7. libcurl安装

    sudo apt-get install libcurl4-openssl-dev -lcurl

  8. mootools和jquery冲突的解决

    mootools-jquery 今天在做EcStore前台的做效果时,由于Jquery的插件比较多,于是就使用了Jquery的插件,但是发现会引起Mootools的冲突. 于是猛找资料,终于找到了,现 ...

  9. php的public、protected、private三种访问控制模式的区别

    public: 公有类型 在子类中可以通过self::var调用public方法或属性,parent::method调用父类方法 在实例中可以能过$obj->var 来调用 public类型的方 ...

  10. C#更改控制台文本颜色

    C#更改控制台文本的前景色和背景色   关键字:C# NET 控制台 前景色 背景色地址:http://www.cnblogs.com/txw1958/archive/2012/12/07/cshar ...