[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,最先 ...
随机推荐
- 非 动态规划---LIS
题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度.(见动态规划---LIS) /* 题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度 ...
- inline-block去掉空白距离的方法
一.现象描述:inline-block形式水平呈现的元素,换行显示或空格分割的情况下,元素之间会有间距,实例如下: 使用CSS将行内元素的display设置为inline-block时,也会出现间隔: ...
- dbo与db_owner区别
dbo 是具有在数据库中执行所有活动的暗示性权限的用户.将固定服务器角色 sysadmin 的任何成员都映射到每个数据库内称为 dbo 的一个特殊用户上.另外,由固定服务器角色 sysadmin 的任 ...
- MFC 进度条控件
1.进度条 主要用来进行数据读写.文件拷贝和磁盘格式等操作时的工作进度提示情况,如安装程序等,伴随工作进度的进展,进度条的矩形区域从左到右利用当前活动窗口标题条的颜色来不断填充. 2.进度条控制在MF ...
- bash
unix - Unlimited Bash History - Stack Overflowhttp://stackoverflow.com/questions/9457233/unlimited-b ...
- android 中通过代码创建控件
package bvb.de.openadbwireless.circle; import android.annotation.TargetApi; import android.app.Activ ...
- Ubuntu12.04 安装Samba
Ubuntu12.04 安装Samba Ubuntu12.04 安装Samba 本教程介绍了在Ubuntu12.04安装Samba文件服务器,以及如何配置它通过SMB协议共享文件,以及如何将用户添加. ...
- Java中Properties类的使用
1.properties介绍 java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值&quo ...
- VPS常用工具
1.命令行工具 putty 在Mac下,可以直接使用超级终端 ssh username@ipaddress 2.可视化上传文件工具 WinSCP 在Mac下,使用 Cyberduck
- linux打开文件数量的查看方法
linux打开文件数量的查看方法 linux打开文件数量的查看方法在网上查到两种查看linux打开文件数量的查看方法,但结果不相同,linux查看文件打开数量是以那个文件或命令为标准呢? 搜索过关于u ...