[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,最先 ...
随机推荐
- yum源的修改
源路径: /etc/yum.repos.d/ 配置文件: 网络搜索 CentOS-Base.repo(默认) 设备搜索 CentOS-Media.repo 将CentOS-Base.repo移除或改名 ...
- TI CC2541的整体目标
1. App端会发送一定数量的byte过来蓝牙, 每2个byte是一个汉字的编码. 2. 拿到汉字编码之后, 统计字符数量, 然后通过SPI, 搜索编码 3. 收到的编码, 每个汉字字符有32个byt ...
- TI CC2541的LED控制
现在终于进入到蓝牙SPI的环节了, 下面还要研究I2C, 所以第一步, 先点灯, 就是GPIO控制吧. 参考一下LEd的初始化: void HalLedInit (void){#if (HAL_LED ...
- exe文件打开方式(恢复EXE文件关联)
文件关联损坏常常是计算机病毒造成的,目前网络上有很多相关修复工具,相对来说,System Repair Engineer 支持的修复格式是比较齐全的,这个工具可以在http://www.kztechs ...
- arduino 蓝牙控制RGB LED灯
/* 日期:2016.9.2 功能:arduino 蓝牙控制RGB LED灯 元件: 跳线公公头 * 8 rgbled, 220欧电阻 蓝牙模块 接线: 蓝牙模块VCC,GND分别接5V,GND;TX ...
- WPF single instance
转自:http://www.cnblogs.com/z_lb/archive/2012/09/16/2687487.html public partial class App : Applicatio ...
- 快速搭建Redis缓存数据库
之前一篇随笔——Redis安装及主从配置已经详细的介绍过Redis的安装于配置.本文要讲的是如何在已经安装过Redis的机器上快速的创建出一个新的Redis缓存数据库. 一.环境介绍 1) Linux ...
- MyEclipse+Android 安装配置
1.先安装M有Eclipse 就是不断点:下一步下一步最后finish 激活:http://blog.my-eclipse.cn/myeclipse-2014-crack.html (该网站上 ...
- c#读取Excel的列名问题
在修改c#读取Excel的时候,遇到了一些小问题,总结下,希望别人不用再浪费时间 读取excel的时候,如果是空行就不读取? SELECT * FROM [DB_ESTATE$] where F2&l ...
- 【转】MySQL外键约束On Delete、On Update各取值的含义
转载地址:http://hi.baidu.com/jxqlovejava/item/3d2cc5b5d689917c244b0920 先看On Delete属性,可能取值如上图为:No Actio ...