📜  在 K 次跳跃中使用恰好 M 个硬币穿过的路径

📅  最后修改于: 2021-10-26 07:01:06             🧑  作者: Mango

给定三个整数NKM 分别代表框数(从 1 到N水平对齐)、允许跳跃的总数和可用硬币总数,任务是打印可以在[1, N]内遍历的路径通过在恰好K次跳跃中使用恰好M 个硬币。如果从位置X跳到位置Y,则使用abs(X – Y)硬币。如果在K次跳跃中不能使用M 个硬币,则打印 -1。
例子:

方法:

可以按照以下步骤使用贪婪方法解决该问题:
重复以下操作,直到K变为零。

  1. 找出N-1M – K – 1的最小值。
  2. 基于上述最小值,根据可用性向左或向右移动减少 K。
  3. 重复上述步骤,直到 K 变为 0。

下面是上述方法的实现:

C++
// C++ program to print
// the path using exactly
// K jumps and M coins
#include 
using namespace std;
 
// Function that print the path
// using exactly K jumps and M coins
void print_path(int N, int jump, int coin)
{
    // If no path exists
    if (jump > coin
        || jump * (N - 1) < coin) {
        cout << "-1" << endl;
    }
    else {
        int pos = 1;
        while (jump > 0) {
 
            // It decides which
            // box to be jump
            int tmp = min(N - 1,
                          coin - (jump - 1));
 
            // It decides whether
            // to jump on left side or
            // to jump on right side
            if (pos + tmp <= N) {
                pos += tmp;
            }
            else {
                pos -= tmp;
            }
 
            // Print the path
            cout << pos << " ";
 
            coin -= tmp;
            jump -= 1;
        }
    }
}
 
// Driver Code
int main()
{
    int N = 5, K = 4, M = 12;
 
    // Function Call
    print_path(N, K, M);
    return 0;
}


Java
// Java program to print the path
// using exactly K jumps and M coins
import java.io.*;
 
class GFG{
 
// Function that print the path
// using exactly K jumps and M coins
static void print_path(int N, int jump,
                              int coin)
{
    // If no path exists
    if (jump > coin || jump * (N - 1) < coin)
    {
        System.out.println("-1");
    }
    else
    {
        int pos = 1;
        while (jump > 0)
        {
     
            // It decides which
            // box to be jump
            int tmp = Math.min(N - 1,
                               coin - (jump - 1));
     
            // It decides whether
            // to jump on left side or
            // to jump on right side
            if (pos + tmp <= N)
            {
                pos += tmp;
            }
            else
            {
                pos -= tmp;
            }
     
            // Print the path
            System.out.print(pos + " ");;
     
            coin -= tmp;
            jump -= 1;
        }
    }
}
     
// Driver Code
public static void main (String[] args)
{
    int N = 5, K = 4, M = 12;
     
    // Function Call
    print_path(N, K, M);
}
}
 
// This code is contributed by shubhamsingh10


Python3
# Python3 program to print the path 
# using exactly K jumps and M coins
 
# Function that pr the path
# using exactly K jumps and M coins
def print_path(N, jump, coin):
 
    # If no path exists
    if (jump > coin or
        jump * (N - 1) < coin):
        print("-1")
     
    else:
        pos = 1;
        while (jump > 0):
 
            # It decides which
            # box to be jump
            tmp = min(N - 1,
                      coin - (jump - 1));
 
            # It decides whether
            # to jump on left side or
            # to jump on right side
            if (pos + tmp <= N):
                pos += tmp;
            else:
                pos -= tmp;
             
            # Print the path
            print(pos, end = " ")
 
            coin -= tmp;
            jump -= 1;
         
# Driver code
N = 5
K = 4
M = 12
 
# Function call
print_path(N, K, M);
     
# This code is contributed by grand_master


C#
// C# program to print the path
// using exactly K jumps and M coins
using System;
 
class GFG{
 
// Function that print the path
// using exactly K jumps and M coins
static void print_path(int N, int jump,
                              int coin)
{
     
    // If no path exists
    if (jump > coin || jump * (N - 1) < coin)
    {
        Console.WriteLine("-1");
    }
     
    else
    {
        int pos = 1;
        while (jump > 0)
        {
             
            // It decides which
            // box to be jump
            int tmp = Math.Min(N - 1,
                            coin - (jump - 1));
     
            // It decides whether
            // to jump on left side or
            // to jump on right side
            if (pos + tmp <= N)
            {
                pos += tmp;
            }
            else
            {
                pos -= tmp;
            }
     
            // Print the path
            Console.Write(pos + " ");
     
            coin -= tmp;
            jump -= 1;
        }
    }
}
     
// Driver Code
public static void Main(String[] args)
{
    int N = 5, K = 4, M = 12;
     
    // Function Call
    print_path(N, K, M);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
5 1 4 5

时间复杂度: O(K)
辅助空间: O(1)