HDOJ-三部曲一(搜索、数学)-1006- Catch That Cow
Catch That Cow
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 48 Accepted Submission(s) : 16
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<cstring>
using namespace std;
int que[1000001]; //数组要开大谢;大数组用全局变量开,不能再函数里开。
int step[1000001]={0};
bool f[1000001]={false};
int n,k; int BFS()
{
int front=0,rear=1;
que[0]=n;
step[0]=0;
f[n]=true;
if(que[front]==k)
return step[front];
while(front<rear)
{
if(que[front]<k&&!f[que[front]+1]) //只有当前的数比k小时,加一才能更接近k。
{
que[rear]=que[front]+1;
step[rear]=step[front]+1;
f[que[rear]]=true;
if(que[rear]==k)
return step[rear];
rear++;
}
if(que[front]-1>=0&&!f[que[front]-1]) //数组不能越界
{
que[rear]=que[front]-1;
step[rear]=step[front]+1;
f[que[rear]]=true;
if(que[rear]==k)
return step[rear];
rear++;
}
if(que[front]<k&&!f[que[front]*2]) //只有当前的数比k小时,乘2才有可能更接近k。
{
que[rear]=que[front]*2;
step[rear]=step[front]+1;
f[que[rear]]=true;
if(que[rear]==k)
return step[rear];
rear++;
}
front++;
}
} int main()
{
while(cin>>n>>k)
{
memset(que,0,sizeof(que));
memset(step,0,sizeof(step));
memset(f,false,sizeof(f));
cout<<BFS()<<endl;
}
}
HDOJ-三部曲一(搜索、数学)-1006- Catch That Cow的更多相关文章
- 【搜索】C - Catch That Cow
#include<stdio.h> #include<string.h> struct A{ int state; int step; }queue[]; // 结构体数组用来 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Catch That Cow(广度优先搜索_bfs)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 48036 Accepted: 150 ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58072 Accepted: 18061 ...
随机推荐
- 《Java程序设计》实验二 实验报告
实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Lin ...
- Mybatis 学习-3
1.设计Dao接口 public interface UserDao { public boolean addUser(User user); } public interface CategoryD ...
- hadoop分布式的环境搭建
版本: 使用hadoop1.1.2 JDK为java7 1.下载hadoop 2.配置hadoop文件 3测试 1.下载hadoop: 1.1 在https://archive.apache.o ...
- Gliffy
http://www.gliffy.com Gliffy 支持在线制作流程图,能够很好的支持中文,基础版本免费.在线制作的思维导图是公开的,高级版本有设置隐私权的权力.可以嵌入博客,办公室应用软件中, ...
- 377. Combination Sum IV——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- BZOJ3933 [CQOI2015]多项式
$\sum_{k = 0} ^ {n} a_kx^k = \sum_{k = 0} ^ {n} b_k(x - t)^k \Leftrightarrow \sum_{k = 0} ^ {n} a_k( ...
- Python 2.7教程
参考:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000
- BroadcastReceiver接收系统广播消息
Android常用的广播Action常量: ACTION_TIME_CHANGED:系统时间被改变. ACTION_DATE_CHANGED:系统日期被改变. ACTION_TIMEZONE_CHAN ...
- Resume Hook SSDT
在HookSSDT中 通过在第4部通过索引将NtOpenProcess 换成 Base[索引] = FakeNtOpenProcess; so 在阻止时应该在ntoskrnl.exe 找到真正的Op ...
- UVa 10561 - Treblecross
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...