BFS --- 模板题
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 36079 | Accepted: 11123 |
Description
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?
Input
N
and
K
Output
Sample Input
5 17
Sample Output
4
Hint
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<cstring>
#include<queue>
using namespace std;
int N,K; struct now
{
int x;
int step;
};
bool vis[]; int BFS(int x)
{
int tx;
now ans,tp;
queue<now> que;
ans.x=x,ans.step=;
vis[x]=true;
que.push(ans);
while(!que.empty())
{
tp=que.front();
que.pop();
tx=tp.x+; //no.1
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
tx=tp.x-; //no.2
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
tx=tp.x*; //no.3
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
}
} int main()
{
while(cin>>N>>K)
{
if(N==K)
{
cout<<""<<endl;
continue;
}
memset(vis,false,sizeof(vis));
cout<<BFS(N)<<endl;
}
return ;
}
当然还可以剪枝一下,比如说:如果人的坐标大于牛的坐标,这时就只需要做-1这一步就可以了。
BFS --- 模板题的更多相关文章
- POJ-2251 Dungeon Master (BFS模板题)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- hdu1242 又又又是逃离迷宫(bfs模板题)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...
- Saving Princess claire_(hdu 4308 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...
- Knight Moves(hdu1372 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others) ...
- 用一道模板题理解多源广度优先搜索(bfs)
题目: //多元广度优先搜索(bfs)模板题详细注释题解(c++)class Solution { int cnt; //新鲜橘子个数 int dis[10][10]; //距离 int dir_x[ ...
- HDU-3549 最大流模板题
1.HDU-3549 Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- Qt中的强制类型转换
在C++开发中经常要进行数据类型的强制转换. 刚开始学习的时候,直接对基本数据类型强制类型转换,如float fnum = 3.14; int num = (int)fnum; 随着C++标准的发展, ...
- 配置DirectX SDK开发环境
创建工程 选择空工程 添加源文件 添加DirectX SDK测试程序 属性配置 添加头文件和库文件路径 D:\Microsoft DirectX SDK (February 2010)\Include ...
- python基础-面向对象编程之多态
面向对象编程之多态以及继承.抽象类和鸭子类型三种表现形式 多态 定义:同一种类型的事物,不同的形态 作用: 多态也称之为"多态性".用于在不知道对象具体类型的情况下,统一对象调用方 ...
- maven仓库报错 sqljdbc4、ojdbc6、tomcat-jdbc-8.5.14
报错:Cannot resolve com.microsoft.sqlserver:sqljdbc4:4.0 和 Missing artifact com.microsoft.sqlserver: ...
- spring-data-redis 关于订阅客户端不断创建新线程的问题
项目中使用了spring-data-redis 实现消息订阅功能,原来的配置是这样子: <redis:listener-container connection-factory="je ...
- home_url()用法小结|wordpress函数
home_url()检索可访问当前站点的URL(推荐将<?php bloginfo('url'); ?>用<?php home_url(); ?>来替代),使用适当的协议返回' ...
- centos定时删除log文件
#!bin/bash #获取年 time=$(date "+%Y") #查找并删除7天前的文件 find /opt/applog/travelsky -type f -mtime ...
- ansible-playbook实例
准备前提 配置ansible主机详情:https://www.cnblogs.com/security-guard/ nginx的安装 编写nginx的自动部署文件nginx.yml hos ...
- Spring---SSH整合(二)
基于Spring---SSH整合,使用SSH编写后台: User模块层 TreeNode.hbm.xml <?xml version="1.0" encoding=" ...
- 链接指示:extren"C"
C++程序有时需要调用其他语言编写的函数,最常见的是调用C语言编写的函数.像所有其他名字一样,其他语言中的函数名字也必须在C++中进行声明,并且该声明必须指定返回类型和形参列表.对于其他语言编写的函数 ...