(简单) 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 ...
随机推荐
- Course
Coursera计算机专业课程列表 巴黎中央理工学院人工视觉中的离散推理和学习 教授 Nikos Paragios & Pawan Kumar Jan 10th 2014 8 ...
- 快学Scala-第五章 类
知识点: 1.简单类和无参方法 class Counter { private var value = 0 //必须初始化字段 def increment() { value += 1} //方法默认 ...
- Python 文件Hash(MD5,SHA1)
import hashlib import os,sys def CalcSha1(filepath): with open(filepath,'rb') as f: sh ...
- 一个设置 material design icon的插件工具
一个设置 material design icon的插件工具 github地址:https://github.com/konifar/android-material-design-icon-gene ...
- JavaScript返回上一页并自动刷新
返回并刷新 <script>alert("恭喜您,操作成功!"); window.location.href=document.referrer; </scrip ...
- 表单提交中记得form表单放到table外面
帝国后台按栏目搜索文章时怎么都不生效 控制台查看原来是 栏目的select的值没有提交过去,原来由于form标签在table标签里面,导致js生成的<select>标签提交失败. 解决办 ...
- PHP获取POST方式的XML数据
今天做微信支付开发,微信服务器回调的时候,会发送XML数据到我的服务器,用以往的POST,GET是获取不到的 百度了一下,应该是 $file_in = file_get_contents(" ...
- windows 系统注册dll文件
使用管理员身份注册:命令提示符 管理员身份运行 32 位系统:regsvr32 %windir%\system32\jscript.dll 64 位系统:regsvr32 %windir%\SysWO ...
- 关于tomcat启动没有进行编译或者编译报错的问题
关于tomcat 的问题 如果项目没有编译 解决方案:1: 把项目刷新一下 然后Clean一下,之后等待右下角编译完成100%2: 有可能tomcat conf 里的配置文件的错误 进入查看下3: 如 ...
- (转)多个mapreduce工作相互依赖处理方法完整实例(JobControl)
多个mapreduce工作相互依赖处理方法完整实例(JobControl) 原文地址:http://mntms.iteye.com/blog/2096456?utm_source=tuicool&am ...