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 ...
随机推荐
- 小而美的js程序
1.获取数字数组最小值的索引 function _getMinKey(arr) { var a = arr[0]; var b = 0; for (var k in arr) { if (arr[k] ...
- win8 卸载IIS
C:\Windows\System32\inetsrv C:\Windows\iis7.log C:\inetpub
- hdu---(2604)Queuing(矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu----(1402)A * B Problem Plus(FFT模板)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Tarjan--LCA算法的个人理解即模板
tarjan---LCA算法的步骤是(当dfs到节点u时): 实际: 并查集+dfs 具体步骤: 1 在并查集中建立仅有u的集合,设置该集合的祖先为u 1 对u的每个孩子v: 1.1 tarj ...
- ARM的启动和中断向量表
启动的方式 对于S3C2440而言,启动的方式有两种,一是Nor Flash方式启动,二是Nand Flash方式启动. 使用Nor Flash方式启动 Nor Flash的地址范围如下 0x0000 ...
- wait(), notify(),sleep详解
在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过synchronized()来实现的,需要说明的是,JAVA的synchronized()方法类似于操作系统概念中的 ...
- linux如何修改主机名
一:#hostname hn1 二:修改/etc/sysconfig/network中的 HOSTNAME=hn1三:修改/etc/hosts文件 127.0.0.1 hn1
- 在express3.0上使用模板
express3.0取消了layout设置,为了能使用模版,经过百度后发现有个express-partials模块可以使用 1:安装 npm install express-partials 模块安装 ...
- java--常用类summary(三)
/* 1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车 ...