博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-4001(3入口のBFS)
阅读量:6607 次
发布时间:2019-06-24

本文共 1785 字,大约阅读时间需要 5 分钟。

4001:Catch That Cow

时间限制:
2000ms
内存限制:
65536kB
描述

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?

输入
Line 1: Two space-separated integers:
N and
K
输出
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.

Tips:

大致题意:

给定两个整数n和k

通过 n+1或n-1 或n*2 这3种操作,使得n==k

输出最少的操作次数

 

#include"iostream"

#include"queue"

#include"cstring"

using namespace std;

bool vis[3*100000];

class pos{

public:

         int step;

         int x;

};

int main(){

         queue<pos> q;

         int n,k;

         while(cin>>n>>k){

                   memset(vis,false,sizeof(vis));

                   pos t1,t2;

                   t1.step=0;

                   t1.x=n;

                   q.push(t1);

                   vis[t1.x]=true;

                   while(!q.empty()){

                   t1=q.front();

                   q.pop();

                   if(t1.x==k){

                            cout<<t1.step<<endl;

                            break;}

                   if(t1.x-1>=0&&!vis[t1.x-1//t1.x-1>=0是剪枝操作,位置可以为0

                            t2.x=t1.x-1;

                            t2.step=t1.step+1;

                            q.push(t2);

                            vis[t1.x-1]=true;

                   }

                   if(t1.x+1<=2*k&&!vis[t1.x+1]){//t1.x+1<=2*k是剪枝操作,范围放大到2*k

                            t2.x=t1.x+1;

                            t2.step=t1.step+1;

                            q.push(t2);

                            vis[t1.x+1]=true;

                   }

                   if(2*t1.x<=2*k&&!vis[2*t1.x]){//2*t1.x<=2*k是剪枝操作,范围放大到2*k

                            t2.x=2*t1.x;

                            t2.step=t1.step+1;

                            q.push(t2);

                            vis[2*t1.x]=true;

                   }

                   }

         }

}

 

转载于:https://www.cnblogs.com/lzhitian/archive/2012/02/08/2342386.html

你可能感兴趣的文章
libjpeg的问题
查看>>
MySQL数据库学习笔记(八)----JDBC入门及简单增删改数据库的操作
查看>>
git 显示多个url地址推送
查看>>
Java Web之Filter
查看>>
HTTP状态码详解
查看>>
Java_动态加载
查看>>
atitti.atiNav 手机导航组件的设计
查看>>
Ubuntu+Apache+PHP+Mysql环境搭建(完整版)
查看>>
Atitit.计算机图形图像图片处理原理与概论attilax总结
查看>>
于ssh端口转发的深入实例[转 - 当当 - 51CTO技术博客
查看>>
从Python安装到语法基础,这才是初学者都能懂的爬虫教程 ...
查看>>
超级AD远程管理软件
查看>>
Oracle数据库安全加固记录
查看>>
安全运维之:Linux系统账户和登录安全
查看>>
【cocos2d-x从c++到js】17:使用FireFox进行JS远程调试
查看>>
Kafka Offset Storage
查看>>
深度学习笔记之CNN(卷积神经网络)基础
查看>>
JAVA设计模式之【原型模式】
查看>>
Hadoop 添加删除数据节点(datanode)
查看>>
33.8. slb configuration
查看>>