***参考Catch That Cow(BFS)
Catch That Cow
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 67 Accepted Submission(s) : 22
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int maxn=; int vis[maxn+];
int n,k; struct node
{
int x,c;
}; int BFS()
{
queue<node> q;
while(!q.empty())
q.pop();
memset(vis,,sizeof(vis));
node cur,next;
cur.x=n,cur.c=;
vis[cur.x]=;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
for(int i=; i<; i++)
{
if(i==)
next.x=cur.x-;
else if(i==)
next.x=cur.x+;
else
next.x=cur.x*;
next.c=cur.c+;
if(next.x==k)
return next.c;
if(next.x>= && next.x<=maxn && !vis[next.x])
{
vis[next.x]=;
q.push(next);
}
}
}
return ;
} int main()
{ freopen("1.txt","r",stdin); while(~scanf("%d%d",&n,&k))
{
if(n>=k)
{
printf("%d\n",n-k);
continue;
}
printf("%d\n",BFS());
}
return ;
}
***参考Catch That Cow(BFS)的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- 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 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
随机推荐
- hdu 4122 Alice's mooncake shop(单调队列)
题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...
- netty初探(2)
上一篇 netty(1) 一.TCP/IP 流式传输 在上文演示了2进制流式传输引起的TCP拆包问题,这里继续演示文本型的传输问题,文本型的可以有以下几种策略 1.1 以特殊字符表示结尾 HTTP协议 ...
- Dynamics CRM 相关资料
links: 1.The Microsoft Dynamics CRM Team Blog 2.申请试用Dynamics CRM 2013 http://www.microsoft.com/zh-cn ...
- Flexslider图片轮播、文字图片相结合滑动切换效果
Flexslider是一款基于的jQuery内容滚动插件.它能让你轻松的创建内容滚动的效果,具有非常高的可定制性.开发者可以使用Flexslider轻松创建各种图片轮播效果.焦点图效果.图文混排滚动效 ...
- LeetCode 328. Odd Even Linked List C#
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 用Karma和Jasmine测试Angular应用
TEST: Before you've written any of the code, you know how you want it to behave. You have a specific ...
- sqlserver负载均衡
http://www.cnblogs.com/gaizai/p/3644510.html
- 解决maven web项目Cannot detect Web Project version. Please specify version of Web Project through...的错误
前面已经创建maven web工程,但是问题来了,创建maven web工程之后会出现如下的错误,在pom.xml文件头部 有以下的错误 Description Resource Path Locat ...
- adb shell dumpsys
adb shell dumpsys activity activities -- class/packagename adb shell dumpsys batterystate --reset ...
- linux下卸载和安装mysql数据库的方法
1.1 MySQL下载 下载地址:http://www.mysql.com/downloads/mysql/5.5.html#downloads 版本:5.1.68 平台:linux general ...