📌  相关文章
📜  找到到达数组末尾的最小移动次数

📅  最后修改于: 2021-10-25 04:54:58             🧑  作者: Mango

给定一个大小为N的数组arr[] ,其中每个元素都来自范围[0, 9] 。任务是从第一个索引开始到达数组的最后一个索引。从i索引我们可以移动到(i – 1) th(i + 1) th或任何j索引,其中j ≠ i并且arr[j] = arr[i]

例子:

方法:从给定的数组构造图,其中图中的节点数将等于数组的大小。图i 的每个节点都将连接到第(i 1)节点、第(i + 1)节点和一个节点j ,使得i ≠ j并且arr[i] = arr[j] 。现在,答案将是构建图中从索引0到索引N – 1的路径中的最小边。
数组 arr[] = {1, 2, 3, 4, 1, 5} 的图形如下图所示:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define N 100005
 
vector gr[N];
 
// Function to add edge
void add_edge(int u, int v)
{
    gr[u].push_back(v);
    gr[v].push_back(u);
}
 
// Function to return the minimum path
// from 0th node to the (n - 1)th node
int dijkstra(int n)
{
    // To check whether an edge is visited or not
    // and to keep distance of vertex from 0th index
    int vis[n] = { 0 }, dist[n];
 
    for (int i = 0; i < n; i++)
        dist[i] = INT_MAX;
 
    // Make 0th index visited and distance is zero
    vis[0] = 1;
    dist[0] = 0;
 
    // Take a queue and push first element
    queue q;
    q.push(0);
 
    // Continue this until all vertices are visited
    while (!q.empty()) {
        int x = q.front();
 
        // Remove the first element
        q.pop();
 
        for (int i = 0; i < gr[x].size(); i++) {
 
            // Check if a vertex is already visited or not
            if (vis[gr[x][i]] == 1)
                continue;
 
            // Make vertex visited
            vis[gr[x][i]] = 1;
 
            // Store the number of moves to reach element
            dist[gr[x][i]] = dist[x] + 1;
 
            // Push the current vertex into the queue
            q.push(gr[x][i]);
        }
    }
 
    // Return the minimum number of
    // moves to reach (n - 1)th index
    return dist[n - 1];
}
 
// Function to return the minimum number of moves
// required to reach the end of the array
int Min_Moves(int a[], int n)
{
 
    // To store the positions of each element
    vector fre[10];
    for (int i = 0; i < n; i++) {
        if (i != n - 1)
            add_edge(i, i + 1);
 
        fre[a[i]].push_back(i);
    }
 
    // Add edge between same elements
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < fre[i].size(); j++) {
            for (int k = j + 1; k < fre[i].size(); k++) {
                if (fre[i][j] + 1 != fre[i][k]
                    and fre[i][j] - 1 != fre[i][k]) {
                    add_edge(fre[i][j], fre[i][k]);
                }
            }
        }
    }
 
    // Return the required minimum number of moves
    return dijkstra(n);
}
 
// Driver code
int main()
{
    int a[] = { 1, 2, 3, 4, 1, 5 };
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << Min_Moves(a, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
import java.util.*;
 
class GFG{
     
static ArrayList<
       ArrayList> gr = new ArrayList<
                                    ArrayList>();
static int N = 100005;
 
// Function to add edge
static void add_edge(int u, int v)
{
    for(int i = 0; i < N; i++)
    {
        gr.add(new ArrayList());
    }
    gr.get(u).add(v);
    gr.get(v).add(u);
}
 
// Function to return the minimum path
// from 0th node to the (n - 1)th node
static int dijkstra(int n)
{
     
    // To check whether an edge is visited
    // or not and to keep distance of
    // vertex from 0th index
    int[] vis = new int[n];
    Arrays.fill(vis, 0);
     
    int[] dist = new int[n];
    for(int i = 0; i < n; i++)
    {
        dist[i] = Integer.MAX_VALUE;
    }
     
    // Make 0th index visited and
    // distance is zero
    vis[0] = 1;
    dist[0] = 0;
     
    // Take a queue and push first element
    Queue q = new LinkedList<>();
    q.add(0);
     
    // Continue this until all vertices
    // are visited
    while (q.size() > 0)
    {
         
        // Remove the first element
        int x = q.poll();
        for(int i = 0; i < gr.get(x).size(); i++)
        {
             
            // Check if a vertex is already
            // visited or not
            if (vis[gr.get(x).get(i)] == 1)
            {
                continue;
            }
             
            // Make vertex visited
            vis[gr.get(x).get(i)] = 1;
             
            // Store the number of moves to
            // reach element
            dist[gr.get(x).get(i)] = dist[x] + 1;
             
            // Push the current vertex into
            // the queue
            q.add(gr.get(x).get(i));
        }
    }
     
    // Return the minimum number of
    // moves to reach (n - 1)th index
    return dist[n - 1];
}
 
// Function to return the minimum number of moves
// required to reach the end of the array
static int Min_Moves(int[] a, int n)
{
     
    // To store the positions of each element
    ArrayList<
    ArrayList> fre = new ArrayList<
                                  ArrayList>();
    for(int i = 0; i < 10; i++)
    {
        fre.add(new ArrayList());
    }
    for(int i = 0; i < n; i++)
    {
        if (i != n - 1)
        {
            add_edge(i, i + 1);
        }
        fre.get(a[i]).add(i);
    }
     
    // Add edge between same elements
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0;
                j < fre.get(i).size();
                j++)
        {
            for(int k = j + 1;
                    k < fre.get(i).size();
                    k++)
            {
                if (fre.get(i).get(j) + 1 !=
                    fre.get(i).get(k) &&
                    fre.get(i).get(j) - 1 !=
                    fre.get(i).get(k))
                {
                    add_edge(fre.get(i).get(j),
                             fre.get(i).get(k));
                }
            }
        }
    }
     
    // Return the required minimum
    // number of moves
    return dijkstra(n);
}
 
// Driver code
public static void main(String[] args)
{
    int[] a = { 1, 2, 3, 4, 1, 5 };
    int n = a.length;
     
    System.out.println(Min_Moves(a, n));
}
}
 
// This code is contributed by avanitrachhadiya2155


Python3
# Python3 implementation of the approach
from collections import deque
N = 100005
 
gr = [[] for i in range(N)]
 
# Function to add edge
def add_edge(u, v):
    gr[u].append(v)
    gr[v].append(u)
 
# Function to return the minimum path
# from 0th node to the (n - 1)th node
def dijkstra(n):
     
    # To check whether an edge is visited
    # or not and to keep distance of vertex
    # from 0th index
    vis = [0 for i in range(n)]
    dist = [10**9 for i in range(n)]
 
    # Make 0th index visited and
    # distance is zero
    vis[0] = 1
    dist[0] = 0
 
    # Take a queue and
    # append first element
    q = deque()
    q.append(0)
 
    # Continue this until 
    # all vertices are visited
    while (len(q) > 0):
        x = q.popleft()
 
        # Remove the first element
        for i in gr[x]:
 
            # Check if a vertex is
            # already visited or not
            if (vis[i] == 1):
                continue
 
            # Make vertex visited
            vis[i] = 1
 
            # Store the number of moves
            # to reach element
            dist[i] = dist[x] + 1
 
            # Push the current vertex
            # into the queue
            q.append(i)
 
    # Return the minimum number of
    # moves to reach (n - 1)th index
    return dist[n - 1]
 
# Function to return the minimum number of moves
# required to reach the end of the array
def Min_Moves(a, n):
 
    # To store the positions of each element
    fre = [[] for i in range(10)]
    for i in range(n):
        if (i != n - 1):
            add_edge(i, i + 1)
 
        fre[a[i]].append(i)
 
    # Add edge between same elements
    for i in range(10):
        for j in range(len(fre[i])):
            for k in range(j + 1,len(fre[i])):
                if (fre[i][j] + 1 != fre[i][k] and
                    fre[i][j] - 1 != fre[i][k]):
                    add_edge(fre[i][j], fre[i][k])
 
    # Return the required
    # minimum number of moves
    return dijkstra(n)
 
# Driver code
a = [1, 2, 3, 4, 1, 5]
n = len(a)
 
print(Min_Moves(a, n))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
    static List> gr = new List>();
    static int N = 100005;
   
    // Function to add edge
    static void add_edge(int u, int v)
    {
        for(int i = 0; i < N; i++)
        {
            gr.Add(new List());
        }
        gr[u].Add(v);
        gr[v].Add(u);
    }
   
    // Function to return the minimum path
    // from 0th node to the (n - 1)th node
    static int dijkstra(int n)
    {
       
        // To check whether an edge is visited
        // or not and to keep distance of
        // vertex from 0th index
        int[] vis = new int[n];
        Array.Fill(vis, 0);
        int[] dist = new int[n];
        for(int i = 0; i < n; i++)
        {
            dist[i] = Int32.MaxValue;
        }
       
        // Make 0th index visited and
        // distance is zero
        vis[0] = 1;
        dist[0] = 0;
         
        // Take a queue and push first element
        Queue q = new Queue();
        q.Enqueue(0);
         
        // Continue this until all vertices
        // are visited
        while(q.Count > 0)
        {
           
            // Remove the first element
            int x = q.Dequeue();
            for(int i = 0; i < gr[x].Count; i++ )
            {
               
                // Check if a vertex is already
                // visited or not
                if(vis[gr[x][i]] == 1)
                {
                    continue;
                }
               
                // Make vertex visited
                vis[gr[x][i]] = 1;
               
                // Store the number of moves to
                // reach element
                dist[gr[x][i]] = dist[x] + 1;
                 
                // Push the current vertex into
                // the queue
                q.Enqueue(gr[x][i]);
            }
        }
       
        // Return the minimum number of
        // moves to reach (n - 1)th index
        return dist[n - 1];
    }
   
    // Function to return the minimum number of moves
    // required to reach the end of the array
    static int Min_Moves(int[] a, int n)
    {
       
        // To store the positions of each element
        List> fre = new List>();
        for(int i = 0; i < 10; i++)
        {
            fre.Add(new List());         
        }
        for(int i = 0; i < n; i++)
        {
            if (i != n - 1)
            {
                add_edge(i, i + 1);
            }
            fre[a[i]].Add(i);
        }
       
        // Add edge between same elements
        for(int i = 0; i < 10; i++)
        {
            for(int j = 0; j < fre[i].Count; j++)
            {
                for(int k = j + 1; k < fre[i].Count; k++)
                {
                    if(fre[i][j] + 1 != fre[i][k] &&
                       fre[i][j] - 1 != fre[i][k])
                    {
                        add_edge(fre[i][j], fre[i][k]);
                    }
                }
            }
        }
       
        // Return the required minimum
        // number of moves
        return dijkstra(n);
    }
   
    // Driver code
    static public void Main ()
    {
        int[] a = { 1, 2, 3, 4, 1, 5 };
        int n = a.Length;
        Console.WriteLine(Min_Moves(a, n));
    }
}
 
// This code is contributed by rag2127


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程