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 ...
随机推荐
- wait(), notify(),sleep详解
在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过synchronized()来实现的,需要说明的是,JAVA的synchronized()方法类似于操作系统概念中的 ...
- FileUpload上传与下载
后台代码: public string connstr = "server=128.1.3.113;database=test;uid=sa;pwd=pass"; protecte ...
- mysql在一台服务器搭建主从
注:本环境事先执行rm -rf /usr/local/mysql 以方便实验. 1. 主与从,类似于A机器和B机器的连接,通过bin_log和rpel_log 进行数据连接 2. 如图所示: 3. ...
- mysql创建PATH快捷
1.使其临时生效 PATH=$PATH:/usr/local/mysql/bin 2.永久生效 编辑/etc/profile 添加第79列 然后source /etc/profile 3.输入命令m ...
- 封装自己的JS库
一.基础知识 1.点击计数 第一种: var aBtn=document.getElementsByTagName('input'); var i=0; for(i=0;i<aBtn.lengt ...
- 两天以来对plsqldev操作的记忆
frist,开机后,打开服务,打开服务,打开服务(重要的事情说三遍). and then, 打开plsqldev,输入TEST账户而不是SYS账户,否则你会被许多未接触到的内容淹没你刚刚创建好的表. ...
- ZOJ 3654 Letty's Math Class 模拟 难度:0
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4844 题意:给你一个只包含中括号和正整数,+,-,结果在longlong范围内 ...
- Python 条件判断 循环
age = 20 if age >= 18: print('your age is', age) print('adult') 根据Python的缩进规则,如果if语句判断是True,就把缩进的 ...
- Program B 暴力求解
Given a sequence of integers S = {S1,S2,...,Sn}, you should determine what is the value of the maxim ...
- 解决python version 2.7 required,which was not find in the registry
程序自动写注册表 http://www.vvivv.com/post-143.html 手工写 http://blog.csdn.net/baikaishui525/article/details/9 ...