(简单) POJ 1278 Catch That Cow,回溯。
Description
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?
也是典型的BFS的题目,我第一次设的数组最大是100000*10,然后过了,第二次100000+5,居然也过了。。。
这个题就是从出发点开始bfs,有三条路可以走,一知道发现牛为止。
代码如下:
#include<iostream>
#include<cstring>
#include<queue> using namespace std; int N,K;
int rem[]; void bfs()
{
queue <int> que;
int t; que.push(N);
rem[N]=; while(!que.empty())
{
t=que.front();
que.pop(); if(t==K)
return; if(t*<=&&rem[t*]==-)
{
rem[t*]=rem[t]+;
que.push(t*);
}
if(t+<=&&rem[t+]==-)
{
rem[t+]=rem[t]+;
que.push(t+);
}
if(t->=&&rem[t-]==-)
{
rem[t-]=rem[t]+;
que.push(t-);
}
}
} int main()
{
ios::sync_with_stdio(false); while(cin>>N>>K)
{
memset(rem,-,sizeof(rem)); if(K<=N)
cout<<N-K<<endl;
else
{
bfs();
cout<<rem[K]<<endl;
}
} return ;
}
(简单) POJ 1278 Catch That Cow,回溯。的更多相关文章
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 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 (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3278 Catch That Cow(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
随机推荐
- Games on a CD
Games on a CD time limit per test 4 seconds memory limit per test 512 megabytes input standard input ...
- OpenGL------在Windows系统中显示文字
增加了两个文件,showline.c, showtext.c.分别为第二个和第三个示例程序的main函数相关部分.在ctbuf.h和textarea.h最开头部分增加了一句#include <s ...
- jquery 功能强大的下拉菜单
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org ...
- 什么是dtd文件,为什么需要
DTD为英文Document Type Definition,中文意思为“文档类定义”.DTD肩负着两重任务:一方面它帮助你编写合法的代码,另一方面它让浏览器正确地显示器代码.也许你会问它们居然有这样 ...
- Android中在activity中弹出一个popwindow
//-----在onCreate方法--中------创建popwindow布局 --pop_item-------------------------- View view=Lay ...
- HDU2952:Counting Sheep(DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- Mysql主从备份、主主备份
简单介绍mysql双机,多机异地热备简单原理实战. 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致. 这样做 ...
- 扫盲: JAVA基本常识
http://java-mzd.iteye.com/blog/838514
- html base2
<html> <body> <h1>My First Web Page</h1> <p id="demo">A Para ...
- Viewpager以及ViewPagerIndicator的相关使用
ViewPagerIndicator开源框架可以用来在ViewPager上方做标题,可以在ViewPager下方做跟随移动的小圆点,这个类库必须和自己的项目在电脑的同一磁盘盘符下,比如都在D盘或者E盘 ...