当前位置: 首页 > news >正文

BD202311夏日漫步(最少步数,BFS或者 Dijstra)

本题链接:码蹄集

题目:

夏日夜晚,小度看着庭院中长长的走廊,萌发出想要在上面散步的欲望,小度注意到月光透过树荫落在地砖上,并且由于树荫的遮蔽度不通,所以月光的亮度不同,为了直观地看到每个格子的亮度,小度用了一些自然数来表示它们的亮度。亮度越高则数字越大,亮度相同的数字相同。

走廊是只有一行地砖的直走廊。上面一共有 nn 个格子,每个格子都被小度给予了一个数字 a_iai​ 来表示它的亮度。

小度现在站在 11 号格子,想要去到 nn 号格子。小度可以正向或反向移动到相邻的格子,每次需要花费 11 的体力。

同时小度还有瞬移的能力,其可以花费 11 的体力来瞬移到与当前格子亮度相同的格子上。而且由于小度视野有限,只能瞬移到在当前格子后的第一次亮度相同的格子上。这也意味着不能反向瞬移

小度想知道,到达 nn 号格子需要花费的最小体力是多少。以此制定一个最优秀的散步方案。

样例:

输入
6
0 1 2 3 1 5

输出
3

思路:

        根据题意,我们将小度移动方向,看成一块图,相邻格子之间作为路线,连接线,我们就可以很方便的,根据 BFS 或者 Dijkstra 来获取最少步数的操作了。

        注意!以后遇到最少步数,最少操作这些关键字,思路得要联想到 BFS 和 Dijkstra 这些最少操作数方面的算法啦!

Dijstra代码详解如下:

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define endl '\n'
#define x first
#define y second
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define INF 0x3f3f3f3f3f3f
#define umap unordered_map
#define uset unordered_set
#define All(x) x.begin(),x.end()
#pragma GCC optimize(3,"Ofast","inline")
#define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;
inline void solve();signed main()
{
//	freopen("a.txt", "r", stdin);IOS;int _t = 1;
//	cin >> _t;while (_t--){solve();}return 0;
}int n,ans;using PII = pair<int,int>;
umap<int,vector<int>>g,cnt; // g  记录连接线  cnt 记录可瞬移的点// Dijkstra 最短路求值
inline void Dijkstra()
{vector<int>dist(n + 1,INF);vector<bool>st(n + 1,false);dist[0] = 0;priority_queue<PII,vector<PII>,greater<PII>>q;q.emplace(PII(0,0));while(q.size()){PII now = q.top();q.pop();if(st[now.y])continue;st[now.y] = true;int a = now.y,dis = now.x;for(int i = 0;i < (int)g[a].size();++i){int j = g[a][i];if(dist[j] > dis + 1) dist[j] = dis + 1;q.emplace(PII(dist[j],j));}}ans = dist[n - 1];
}inline void solve()
{cin >> n;uset<int>node;	// 记录 所有点for(int i = 0,x;i < n;++i){cin >> x;node.emplace(x);cnt[x].emplace_back(i);	// 记录可瞬移的点if(i - 1 >= 0){// 小度可以正向或反向移动到相邻的格子g[i].emplace_back(i - 1);g[i - 1].emplace_back(i);}}for(auto &i:node){for(int j = 0;j + 1 < (int)cnt[i].size();++j){// 不能反向瞬移,只能瞬移到在当前格子后的第一次亮度相同的格子上// 所以我们只连接相邻可瞬移的g[cnt[i][j]].emplace_back(cnt[i][j + 1]);}}Dijkstra();cout << ans << endl;
}

最后提交:​​​​​​​​​​​​​​

BFS代码详解如下:

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define endl '\n'
#define x first
#define y second
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define INF 0x3f3f3f3f3f3f
#define umap unordered_map
#define uset unordered_set
#define All(x) x.begin(),x.end()
#pragma GCC optimize(3,"Ofast","inline")
#define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;
inline void solve();signed main()
{
//	freopen("a.txt", "r", stdin);IOS;int _t = 1;
//	cin >> _t;while (_t--){solve();}return 0;
}int n,ans;using PII = pair<int,int>;
umap<int,vector<int>>g,cnt; // g  记录连接线  cnt 记录可瞬移的点// BFS 求最短路
inline int BFS()
{vector<bool>st(N,false);queue<int>q;int step = 0;q.emplace(0);while(q.size()){int sz = q.size();while(sz--){int now = q.front();q.pop();st[now] = true;if(now == n - 1) return step;for(int i = 0;i < (int)g[now].size();++i){int j = g[now][i];if(!st[j]){st[j] = true;q.emplace(j);}}}++step;}return 0;
}inline void solve()
{cin >> n;uset<int>node;	// 记录 所有点for(int i = 0,x;i < n;++i){cin >> x;node.emplace(x);cnt[x].emplace_back(i);	// 记录可瞬移的点if(i - 1 >= 0){// 小度可以正向或反向移动到相邻的格子g[i].emplace_back(i - 1);g[i - 1].emplace_back(i);}}for(auto &i:node){for(int j = 0;j + 1 < (int)cnt[i].size();++j){// 不能反向瞬移,只能瞬移到在当前格子后的第一次亮度相同的格子上// 所以我们只连接相邻可瞬移的g[cnt[i][j]].emplace_back(cnt[i][j + 1]);}}int ans = BFS();cout << ans << endl;
}

最后提交:

http://www.lryc.cn/news/337958.html

相关文章:

  • React - 你知道props和state之间深层次的区别吗
  • mysql 查询实战-变量方式-解答
  • SpringBoot3配置SpringSecurity6
  • Unity之Unity面试题(三)
  • Linux命令-dos2unix命令(将DOS格式文本文件转换成Unix格式)
  • 企业怎么做数据分析
  • 1111111111
  • [面向对象] 单例模式与工厂模式
  • 《前端防坑》- JS基础 - 你觉得typeof nullValue === null 么?
  • 【项目实战经验】DataKit迁移MySQL到openGauss(下)
  • AI预测体彩排3第2弹【2024年4月13日预测--第1套算法开始计算第2次测试】
  • 【13137】质量管理(一)2024年4月串讲题组一
  • Go语言中工作负载类型对并发的影响
  • 常用的Python内置函数
  • MAC(M1芯片)编译Java项目慢且发热严重问题解决方案
  • 如何循环pandas格式的数据
  • 新零售SaaS架构:客户管理系统架构设计(万字图文总结)
  • Apache Spark
  • CentOS7编译ZLMediaKit并使能WebRTC
  • 【数据交换格式】网络socket编程温度采集智能存储与上报项目技术------JSON、TLV
  • IP地址定位技术在各领域的作用
  • 代码随想录 538. 把二叉搜索树转换为累加树
  • JavaWeb--前端--01HTML和CSS
  • Oracle SQL中的DECODE函数与NVL函数:区别与应用场景详析
  • 算法设计与分析实验报告c++实现(N皇后问题、卫兵布置问题、求解填字游戏问题、图的m着色问题)
  • 深入探索Linux中的libgdbus:GDBus库的应用和实现
  • MacOS下Qt 5开发环境安装与配置
  • jquery 实现倒计时
  • MYSQL 5.7重置root密码
  • 博客永久链接与计数