本题链接:码蹄集
题目:
夏日夜晚,小度看着庭院中长长的走廊,萌发出想要在上面散步的欲望,小度注意到月光透过树荫落在地砖上,并且由于树荫的遮蔽度不通,所以月光的亮度不同,为了直观地看到每个格子的亮度,小度用了一些自然数来表示它们的亮度。亮度越高则数字越大,亮度相同的数字相同。
走廊是只有一行地砖的直走廊。上面一共有 n 个格子,每个格子都被小度给予了一个数字 来表示它的亮度。
小度现在站在 1 号格子,想要去到 n 号格子。小度可以正向或反向移动到相邻的格子,每次需要花费 1 的体力。
同时小度还有瞬移的能力,其可以花费 1 的体力来瞬移到与当前格子亮度相同的格子上。而且由于小度视野有限,只能瞬移到在当前格子后的第一次亮度相同的格子上。这也意味着不能反向瞬移。
小度想知道,到达 n 号格子需要花费的最小体力是多少。以此制定一个最优秀的散步方案。
样例:
|
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;
}
最后提交:![](https://img-blog.csdnimg.cn/direct/efc8e3a6a20a406f96f9b5b361da4b01.png)
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;
}