📜  须藤放置[1.3] |最终目的地

📅  最后修改于: 2022-05-13 01:57:01.004000             🧑  作者: Mango

须藤放置[1.3] |最终目的地

给定一个整数数组和一个带有初始值和最终值的数字 K。您的任务是使用数组元素找到从初始值开始获得最终值所需的最少步骤数。您只能对值进行添加(添加操作 % 1000)以获得最终值。在每一步,您都可以使用模数运算添加任何数组元素。

例子:

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

方法:由于在上述问题中给定的模数为 1000,因此状态的最大数量将为 10 3 。可以使用简单的 BFS 检查所有状态。用 -1 初始化 ans[] 数组,这标志着尚未访问过该状态。 ans[i] 存储从开始到达 i 所采取的步数。最初将开始推入队列,然后应用 BFS。弹出顶部元素并检查它是否等于结尾,如果是,则打印 ans[end]。如果元素不等于最顶层元素,则将顶层元素与数组中的每个元素相加,并以1000进行模运算。如果之前未访问过添加的元素状态,则将其推入队列。将 ans[pushed_element] 初始化为 ans[top_element] + 1。一旦访问了所有状态,并且无法通过执行所有可能的乘法来达到该状态,则打印 -1。

下面是上述方法的实现:

C++
// C++ program to find the minimum steps
// to reach end from start by performing
// additions and mod operations with array elements
#include 
using namespace std;
 
// Function that returns the minimum operations
int minimumAdditions(int start, int end, int a[], int n)
{
    // array which stores the minimum steps
    // to reach i from start
    int ans[1001];
 
    // -1 indicated the state has not been visited
    memset(ans, -1, sizeof(ans));
    int mod = 1000;
 
    // queue to store all possible states
    queue q;
 
    // initially push the start
    q.push(start % mod);
 
    // to reach start we require 0 steps
    ans[start] = 0;
 
    // till all states are visited
    while (!q.empty()) {
 
        // get the topmost element in the queue
        int top = q.front();
 
        // pop the topmost element
        q.pop();
 
        // if the topmost element is end
        if (top == end)
            return ans[end];
 
        // perform addition with all array elements
        for (int i = 0; i < n; i++) {
            int pushed = top + a[i];
            pushed = pushed % mod;
 
            // if not visited, then push it to queue
            if (ans[pushed] == -1) {
                ans[pushed] = ans[top] + 1;
                q.push(pushed);
            }
        }
    }
    return -1;
}
 
// Driver Code
int main()
{
    int start = 998, end = 2;
    int a[] = { 2, 1, 3 };
    int n = sizeof(a) / sizeof(a[0]);
 
    // Calling function
    cout << minimumAdditions(start, end, a, n);
    return 0;
}


Java
// Java program to find the minimum steps
// to reach end from start by performing
// additions and mod operations with array elements
import java.util.*;
 
class GFG
{
 
    // Function that returns
    // the minimum operations
    static int minimumAdditions(int start,
                    int end, int a[], int n)
    {
        // array which stores the minimum steps
        // to reach i from start
        int ans[] = new int[1001];
 
        // -1 indicated the state has not been visited
        Arrays.fill(ans, -1);
        int mod = 1000;
 
        // queue to store all possible states
        Queue q = new java.util.LinkedList<>();
 
        // initially push the start
        q.add(start % mod);
 
        // to reach start we require 0 steps
        ans[start] = 0;
 
        // till all states are visited
        while (!q.isEmpty())
        {
 
            // get the topmost element in the queue
            int top = q.peek();
 
            // pop the topmost element
            q.poll();
 
            // if the topmost element is end
            if (top == end)
            {
                return ans[end];
            }
 
            // perform addition with all array elements
            for (int i = 0; i < n; i++)
            {
                int pushed = top + a[i];
                pushed = pushed % mod;
 
                // if not visited, then push it to queue
                if (ans[pushed] == -1)
                {
                    ans[pushed] = ans[top] + 1;
                    q.add(pushed);
                }
            }
        }
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int start = 998, end = 2;
        int a[] = {2, 1, 3};
        int n = a.length;
 
        // Calling function
        System.out.println(minimumAdditions(start, end, a, n));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 program to find the minimum steps
# to reach end from start by performing
# additions and mod operations with array
# elements
from collections import deque
from typing import List
 
# Function that returns the minimum operations
def minimumAdditions(start: int, end: int,
                     a: List[int], n: int) -> int:
                          
    # Array which stores the minimum steps
    # to reach i from start
    # -1 indicated the state has not been visited
    ans = [-1] * 1001
 
    mod = 1000
 
    # Queue to store all possible states
    q = deque()
 
    # Initially push the start
    q.append(start % mod)
 
    # To reach start we require 0 steps
    ans[start] = 0
 
    # Till all states are visited
    while q:
         
        # Get the topmost element in the queue
        top = q[0]
 
        # Pop the topmost element
        q.popleft()
 
        # If the topmost element is end
        if (top == end):
            return ans[end]
 
        # Perform addition with all array elements
        for i in range(n):
            pushed = top + a[i]
            pushed = pushed % mod
 
            # If not visited, then push it to queue
            if (ans[pushed] == -1):
                ans[pushed] = ans[top] + 1
                q.append(pushed)
 
    return -1
 
# Driver Code
if __name__ == "__main__":
 
    start = 998
    end = 2
    a = [ 2, 1, 3 ]
    n = len(a)
 
    # Calling function
    print(minimumAdditions(start, end, a, n))
 
# This code is contributed by sanjeev2552


C#
// C# program to find the minimum steps
// to reach end from start by performing
// additions and mod operations with array elements
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function that returns
    // the minimum operations
    static int minimumAdditions(int start,
                    int end, int []a, int n)
    {
        // array which stores the minimum steps
        // to reach i from start
        int []ans = new int[1001];
         
        // -1 indicated the state has not been visited
        for(int i = 0; i < 1001; i++)
        {
            ans[i] = -1;
        }
        int mod = 1000;
 
        // queue to store all possible states
        Queue q = new Queue();
 
        // initially push the start
        q.Enqueue(start % mod);
 
        // to reach start we require 0 steps
        ans[start] = 0;
 
        // till all states are visited
        while (q.Count != 0)
        {
 
            // get the topmost element in the queue
            int top = q.Peek();
 
            // pop the topmost element
            q.Dequeue();
 
            // if the topmost element is end
            if (top == end)
            {
                return ans[end];
            }
 
            // perform addition with all array elements
            for (int i = 0; i < n; i++)
            {
                int pushed = top + a[i];
                pushed = pushed % mod;
 
                // if not visited, then push it to queue
                if (ans[pushed] == -1)
                {
                    ans[pushed] = ans[top] + 1;
                    q.Enqueue(pushed);
                }
            }
        }
        return -1;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int start = 998, end = 2;
        int []a = {2, 1, 3};
        int n = a.Length;
 
        // Calling function
        Console.WriteLine(minimumAdditions(start, end, a, n));
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:
2

时间复杂度:O(N)