poj 3278(hdu 2717) Catch That Cow(bfs)
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9753 Accepted Submission(s): 3054
* 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define M 100005
using namespace std;
int n,m;
int visit[M]; //标记数组,0表示没走过,1表示已走过
struct node
{
int x,t;
} s1,s2;
void BFS()
{
queue<node> q;
while(!q.empty())
q.pop();
s1.x=n;
s1.t=;
visit[n]=; //农民起始位置标记为已走过
q.push(s1);
while(!q.empty())
{
s1=q.front();
q.pop();
if(s1.x==m) //结束标志为农民到了牛的位置
{
printf("%d\n",s1.t);
return;
}
s2.x=s1.x+; //坐标+1
if(s2.x>=&&s2.x<=M&&!visit[s2.x]) //判断变化后的数字是否超过了范围
{
visit[s2.x]=;
s2.t=s1.t+;
q.push(s2);
}
s2.x=s1.x-; //坐标-1
if(s2.x>=&&s2.x<=M&&!visit[s2.x])
{
visit[s2.x]=;
s2.t=s1.t+;
q.push(s2);
}
s2.x=s1.x*; //坐标*2
if(s2.x>=&&s2.x<=M&&!visit[s2.x])
{
visit[s2.x]=;
s2.t=s1.t+;
q.push(s2);
}
}
}
int main()
{
int i,j;
while(~scanf("%d %d",&n,&m))
{
memset(visit,,sizeof(visit)); //开始全部定义为0
BFS();
}
return ;
}
poj 3278(hdu 2717) Catch That Cow(bfs)的更多相关文章
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- 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 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ 3278 Catch That Cow(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
- Catch That Cow(BFS)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- bzoj 4521 [Cqoi2016]手机号码——数位dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4521 dfs真好用~ #include<iostream> #include&l ...
- arcgis增大缩放级别
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Thread.sleep
Thread.sleep() The current thread changes state from Running to Waiting/Blocked as shown in the diag ...
- git解决冲突的最佳方法
用eclipse egit 去pull 代码出现冲突 点击details 全选复制到记事本上 如上图选择3个冲突中的一个 eclipse快捷键 ctrl + shift+R 张贴 回车 ...
- PHP协程:并发 shell_exec
在PHP程序中经常需要用shell_exec执行一些命令,而普通的shell_exec是阻塞的,如果命令执行时间过长,那可能会导致进程完全卡住.在Swoole4协程环境下可以用Co::exec并发地执 ...
- PLAY2.6-SCALA(十一) 模板常用场景
1.布局 声明一个views/main.scala.html模板作为主布局模板 @(title: String)(content: Html) <!DOCTYPE html> <ht ...
- github遇到 non-fast-forward错误
可以参考:这里 解决过程如图所示
- Spring_Aop_(二)
切面的优先级 @Order(1)注解 指定切面的优先级,值越小优先级越高 @Order(1) @Aspect @Component public class VlidationAspect { @Be ...
- yum源配置及详解
红帽系列中,进行软件安装可以有三种方法,编译安装,rpm包安装,和yum源安装.其中yum方法安装最简单,因为它可以自动解决软件包之间的依赖关系... 一.常用yum源 yum源可以来源于多种文件 ...
- kubernetes1.4新特性:支持sysctl命令
背景介绍 sysctl是一个允许改变正在运行中的Linux系统内核参数的接口.可以通过sysctl修改Linux系统内核中的TCP/IP 堆栈和虚拟内存系统的高级选项,而且不需要重新启动Linux系统 ...