计蒜客 一维坐标的移动(BFS)
2024-08-30 02:25:50
在一个长度为 n 的坐标轴上,蒜头君想从 A 点 移动到 B 点。他的移动规则如下:
- 向前一步,坐标增加 1。
- 向后一步,坐标减少 1。
- 跳跃一步,使得坐标乘 2。
蒜头君不能移动到坐标小于 0 或大于 n 的位置。蒜头想知道从 A 点移动到 B 点的最少步数是多少,你能帮他计算出来么?
输入格式
第一行输入三个整数 n,A,B,分别代表坐标轴长度,起始点坐标,终点坐标。(50000≤A,B≤n≤5000)
输出格式
输出一个整数占一行,代表蒜头要走的最少步数。
样例输入
样例输出
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI = acos(-);
#define Bug cout<<"---------------------"<<endl
const int maxn=1e4+;
using namespace std; int vis[]; int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); int n,a,b;
scanf("%d %d %d",&n,&a,&b);
int ans=;
if(a>=b)
ans=a-b;
else //BFS
{
queue<pair<int,int> > qe;
vis[a]=;
qe.push(make_pair(a,));
while(!qe.empty())
{
int now=qe.front().first;
int step=qe.front().second;
qe.pop();
if(now==b)//找到了
{
ans=step;
break;
}
int to;
to=now+;//前进1步
if(to>=&&to<=n&&!vis[to])
{
vis[to]=;
qe.push(make_pair(to,step+));
}
to=now-;//后退1步
if(to>=&&to<=n&&!vis[to])
{
vis[to]=;
qe.push(make_pair(to,step+));
}
to=now*;//跳跃一步
if(to>=&&to<=n&&!vis[to])
{
vis[to]=;
qe.push(make_pair(to,step+));
} }
}
printf("%d\n",ans); return ;
}
-
最新文章
- ArcMap常用VBA
- C#笔试(程序设计)
- delphi7 编译程序时报win32.indcu.a病毒的解决方法
- windows 下配置 nginx的问题
- 【linux命令】:查看当前登录用户的信息,本文介绍3种方法
- shell中大小写转换
- 在Eclipse中制作SSH配置文件提示插件
- HW4.3
- 【转】 Android的NDK开发(1)————Android JNI简介与调用流程
- stack适配栈
- Graph.js
- haproxy image跳转 haproxy匹配 匹配到了就停止,不会继续往下匹配
- c# 数据库编程(通过SqlCommand 执行数据库查询)
- OpenCV探索之路(十八):使用imwrite调整保存的图片质量
- 图像边缘检测--OpenCV之cvCanny函数
- XML语言2.约束
- Python第十课学习
- file 文件上传,下载,删除
- Parameter not found的出现的原因
- virtual box 5.2.12 扩展包安装
热门文章
- 十、JavaScript之文本相加
- 二、环境安装:yarn
- 第九篇 AJAX
- 【转载】使用driver.findElement(By.id(";txtPhoneNum";)).getText();获取文本
- 每天一点点之vue框架开发 - 部署到线上
- JS图片多个上传,并压缩为Base64
- Android中时间戳的详细解释
- Abstract抽象类 &;&; Interface接口
- 爬虫(十八):Scrapy框架(五) Scrapy通用爬虫
- 浅谈ASCII 、ISO8859-1、GB2312、GBK、Unicode、UTF-8 的区别。