BFS
const int INF = 0x3f3f3f3f;
class Solution {
public:
int minJumps(vector<int>& arr) {
int n = arr.size();
unordered_map<int, vector<int>> hash;
for(int i = 0; i < n; i ++)
hash[arr[i]].push_back(i);
vector<int> dist(n, INF);
queue<int> q;
dist[0] = 0;
q.push(0);
while(!q.empty())
{
int t = q.front();
q.pop();
//左边位置和右边位置
for(int i = t - 1; i <= t + 1; i += 2)
if(i >= 0 && i < n && dist[i] > dist[t] + 1)
{
dist[i] = dist[t] + 1;
q.push(i);
}
// 值相同的位置
int w = arr[t];
if(hash.count(w))
{
for(int i : hash[w])
{
if(dist[i] > dist[t] + 1)
{
dist[i] = dist[t] + 1;
q.push(i);
}
}
hash.erase(w); //不重复搜索
}
}
return dist[n - 1];
}
};