poj 3278 Catch That Cow(bfs)

                                                                                                    

                                                   Catch That Cow

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 89208

Accepted: 27970

Description

Farmer John has been informed of the location of afugitive 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 oftransportation: walking and teleporting.

* Walking: FJ can move from any point X to thepoints X - 1 orX + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X ina single minute.

If the cow, unaware of its pursuit, does not move atall, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N andK

Output

Line 1: The least amount of time, in minutes, it takesfor Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitivecow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

 

 

简要题意:

有一个农夫和一头牛,在一个数轴上,位置用整数N,K表示。农夫有三种移动方式,从位置为N可以移动到N-1、N+1、和2N的位置,每种方式耗时均为单位时间1,牛固定在自己的位置不移动,求农夫抓到牛所用的最短时间。

分析:把数轴可以看作一个有向图,每个整数视作一个顶点,相邻整数之间互相连通,而N只能单向连通2N,用bfs搜索最短路径即可。注意标记每个顶点是否被访问过,被访问过后不需要再次访问,否则不是最优解。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <math.h>
using namespace std;

//bfs搜索

struct point
{
    int x;  //记录当前位置
    int step;   //记录步数
    point(int xx,int ss):x(xx),step(ss){}; //初始化
};
const int MAXN=200000+5;
bool vis[MAXN];  //标记每个顶点
queue <point> temp;
int main()
{
    int N,K;
    scanf("%d %d",&N,&K);
    temp.push(point(N,0));
    memset(vis,false,sizeof(vis));
    while(!temp.empty())
    {
        point cur=temp.front();
        vis[cur.x]=true; //访问该点
        if(cur.x==K)     //到达目的点,返回答案
        {
           printf("%d\n",cur.step);
           break;
        }
        if(cur.x-1>=0 && !vis[cur.x-1])   //注意越界判断,把三种方式压入队列中
            temp.push(point(cur.x-1,cur.step+1));
        if(cur.x+1<=100000 && !vis[cur.x+1])
            temp.push(point(cur.x+1,cur.step+1));
        if(cur.x*2<=100000 && !vis[cur.x*2])
            temp.push(point(cur.x*2,cur.step+1));
        temp.pop(); //弹出队首元素
    }
    return 0;
}


本项目是一个基于SSM(Spring+SpringMVC+MyBatis)后端框架与Vue.js前端框架开发的疫情居家办公系统。该系统旨在为居家办公的员工提供一个高效、便捷的工作环境,同时帮助企业更好地管理远程工作流程。项目包含了完整的数据库设计、前后端代码实现以及详细的文档说明,非常适合计算机相关专业的毕设学生和需要进行项目实战练习的Java学习者。 系统的核心功能包括用户管理、任务分配、进度跟踪、文件共享和在线沟通等。用户管理模块允许管理员创建和管理用户账户,分配不同的权限。任务分配模块使项目经理能够轻松地分配任务给团队成员,并设置截止日期。进度跟踪模块允许员工实时更新他们的工作状态,确保项目按计划进行。文件共享模块提供了一个安全的平台,让团队成员可以共享和协作处理文档。在线沟通模块则支持即时消息和视频会议,以增强团队之间的沟通效率。 技术栈方面,后端采用了Spring框架来管理业务逻辑,SpringMVC用于构建Web应用程序,MyBatis作为ORM框架简化数据库操作。前端则使用Vue.js来实现动态用户界面,搭配Vue Router进行页面导航,以及Vuex进行状态管理。数据库选用MySQL,确保数据的安全性和可靠性。 该项目不仅提供了一个完整的技术实现示例,还为开发者留下了扩展和改进的空间,可以根据实际需求添加新功能或优化现有功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值