[HDOJ2717]Catch That Cow
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8616 Accepted Submission(s): 2714
* 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?
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
经典BFS,常规思路。
注意:
题目所给5 17和17 5答案是不一样的。我曾天真地认为需要进行一步swap,实际上是不需要的。
遍历到某点应判断是否越界,否则也会ACCESS_VIOLATION。
数组也要开大一点,否则也会ACCESS_VIOLATION。
(PS:我忘记修改判断是否越界时界限的大小。)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int vis[];
int stp[];
int n, k; void swap(int& a, int& b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
} /*
int move(int sgn, int cur)
{
if(sgn == 1)
{
return cur + 1;
}
if(sgn == -1)
{
return cur - 1;
}
else
{
return cur * 2;
}
}
*/ void BFS(int ini)
{
int cur, now = ; //init
queue<int> q;
vis[ini] = ;
stp[ini] = ;
q.push(ini);
while(!q.empty())
{
cur = q.front();
q.pop();
for(int i = ; i < ; i++)
{
if(i == )
{
now = cur + ;
}
else if(i == )
{
now = cur - ;
}
else
{
now = cur * ;
} if(!vis[now] && now >= && now <= )
{
vis[now] = ;
q.push(now);
stp[now] = stp[cur] + ;
}
if(now == k)
{
printf("%d\n", stp[now]);
return ;
}
}
}
}
int main()
{
while(scanf("%d %d", &n, &k) != EOF && n+k)
{
memset(vis, , sizeof(vis));
memset(stp, , sizeof(stp));
if (n == k)
{
printf("0\n");
}
else
{
BFS(n);
}
}
return ;
}
[HDOJ2717]Catch That Cow的更多相关文章
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 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 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- 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 ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
随机推荐
- yii2获取登陆的用户名
yii2获取登陆的用户名: yii::$app->user->identity->username; 判断用户名是否登陆 if(Yii::$app->user->isGu ...
- UIPage
分页控件是一种用来取代导航栏的可见指示器,方便手势直接翻页,最典型的应用便是iPhone的主屏幕,当图标过多会自动增加页面,在屏幕底部你会看到原点,用来只是当前页面,并且会随着翻页自动更新. 一.创建 ...
- 学习Perl6: slice fastq file
需求: 只获取 ath 物种的 hairpin 序列 文件格式如下所示,以>打头的为 header,紧跟的为序列[AUCG]+ (Perl5 regexp 格式) #!/usr/bin/env ...
- Hibernate解决n+1问题
观点:对于n+1问题的理解. 一般而言说n+1意思是,无论在一对多还是多对一当查询出n条数据之后,每条数据会关联的查询1次他的关联对象,这就叫做n+1. 但是我的理解是,本来所有信息可以一次性查询出来 ...
- 转Class.forName()用法详解
主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...
- C#:WiFi
写的一个简单启动关闭WiFi的类:具体如下 using System; using System.Collections.Generic; using System.Text; using Syste ...
- APP运营推广那点事【干货】
你的手机里面有多少应用?什么样的手机应用吸引你?下载之后经常用还是让他shi在那里?又或者刚点进去就卸载? 一款成功的应用,开发APP只是第一步,比前者更重要的是“养”APP,APP就像是一个需要不断 ...
- MS Sq l数据类型
一.nchar : 适用于西文字符,一个字符占一个字节. 二.char: 可以中文,一个字符占两个字节. 三.var :可变. 四.varchar.nvarchar . 所以一般来说,如果含有中文字符 ...
- [HTML]HTML5实现可编辑表格
HTML5实现的简单的可编辑表格 [HTML]代码 <!DOCTYPE html > <html > <head> <meta charset="u ...
- [团队项目]后续安排 Github
6.后续安排 第16周 周二晚7点之前将本代码上传到GITHUB. 周三上课时运行你们的系统给我观赏一下. 根据博客,运行演示,github代码情况评定第二个冲刺的分数. 至此,软件工程学期平时分截止 ...