POJ-3278 Catch that cow
来源:互联网 发布:马氏链模型matlab编程 编辑:程序博客网 时间:2024/06/07 03:39
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (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 orX+ 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?
Line 1: Two space-separated integers: N andK
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
5 17
4
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<queue> using namespace std; queue<int>sm;//宽搜用队列 int d[200001]={0},start,end;//d[]代表走到当前点需要的最少步数 int search (){ sm.push(start); for(int i=0;i<=200000;i++)d[i]=1001110;//初始化 d[start]=0; while(sm.size()){ int p=sm.front();sm.pop(); if(p==end)break; if((p-1)>=0&&d[p]+1<d[p-1]){//边界条件是0 d[p-1]=d[p]+1; sm.push(p-1); } if(p<n&&d[p]+1<d[p+1]){//若小于n才能+1 d[p+1]=d[p]+1; sm.push(p+1); } if(p<100001&&d[p]+1<d[2*p]){//小于n才能乘2 d[p*2]=d[p]+1; sm.push(p*2); } } return d[end]; } int main(){
cin>>start>>end; cout<<search(); }
阅读全文
1 0
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 catch that cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- Poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 - Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ-3278-Catch That Cow
- JVM调优:选择合适的GC collector (一)
- MOOC清华《程序设计基础》第6章第3题:最大乘积问题(动态规划)
- [leetcode]310. Minimum Height Trees
- SpringMVC与ajax交互流程与实现
- 矩阵键盘
- POJ-3278 Catch that cow
- git学习------>从SVN迁移到Git之后,项目开发代码继续在SVN提交,如何同步迁移之后继续在SVN提交的代码到Git?
- Spring4.3+Hibernate5.2.10整合
- 数独-dfs
- java 实现二叉树【递归/非递归】
- bzoj 3208: 花神的秒题计划Ⅰ
- 为何将INF设置为0x3f3f3f3f
- Python多线程--(1)之基本概念
- shape与reshape