📜  使用K个跳跃中的M个硬币精确遍历的路径

📅  最后修改于: 2021-04-27 18:56:33             🧑  作者: 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


输出:
5 1 4 5

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