📜  玩家可以在硬币游戏中收集的最大金额

📅  最后修改于: 2021-09-03 15:06:47             🧑  作者: Mango

给定一个由N行组成的二维数组Arr[][]和两个人AB根据以下规则进行轮流游戏:

  • 随机选择一行,其中A只能拿走最左边的硬币,而B只能拿走所选行中最右边的硬币。
  • 当没有剩余硬币时,游戏结束。

任务是确定A获得的最大金额。

例子:

  处理方法:按照以下步骤解决问题

  1. 在以最优策略玩的游戏中
  2. 初始化一个变量,比如amount ,以存储A获得的钱。
  3. 如果N是偶数, A将收集前一半的硬币
  4. 否则,首先, A将收集(N / 2) 个硬币, B将收集最后一个(N / 2)
  5. 如果 N 为奇数,则中间的硬币可由AB收集,具体取决于移动顺序。
  6. 将硬币存储在所有奇数行中间的变量中,例如mid_odd[]。
  7. 按降序对数组mid_odd[]进行排序。
  8. 最佳情况下, A将收集min_odd[]偶数索引处的所有硬币
  9. 最后,打印A的分数。

下面是上述方法的实现:

C++
// CPP Program to implement
// the above approach
#include
using namespace std;
 
// Function to calculate the
// maximum amount collected by A
void find(int N,  vector>Arr)
{
     
    // Stores the money
    // obtained by A
    int amount = 0;
 
    // Stores mid elements
    // of odd sized rows
    vector mid_odd;
    for(int i = 0; i < N; i++)
    {
 
        // Size of current row
        int siz = Arr[i].size();
 
        // Increase money collected by A
        for (int j = 0; j < siz / 2; j++)
            amount = amount + Arr[i][j];
 
        if(siz % 2 == 1)
            mid_odd.push_back(Arr[i][siz/2]);
    }
 
    // Add coins at even indices
    // to the amount collected by A
    sort(mid_odd.begin(),mid_odd.end());
 
    for(int i = 0; i < mid_odd.size(); i++)
        if (i % 2 == 0)
         amount = amount + mid_odd[i];
 
    // Print the amount
    cout<>Arr{{5, 2, 3, 4}, {1, 6}};
 
   // Function call to calculate the
   // amount of coins collected by A
   find(N, Arr);
}
 
// This code is contributed by ipg2016107.


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
  
// Function to calculate the
// maximum amount collected by A
static void find(int N, int[][] Arr)
{
     
    // Stores the money
    // obtained by A
    int amount = 0;
 
    // Stores mid elements
    // of odd sized rows
    ArrayList mid_odd
            = new ArrayList();
    for(int i = 0; i < N; i++)
    {
 
        // Size of current row
        int siz = Arr[i].length;
 
        // Increase money collected by A
        for (int j = 0; j < siz / 2; j++)
            amount = amount + Arr[i][j];
 
        if(siz % 2 == 1)
            mid_odd.add(Arr[i][siz/2]);
    }
 
    // Add coins at even indices
    // to the amount collected by A
    Collections.sort(mid_odd);
     
    for(int i = 0; i < mid_odd.size(); i++){
        if (i % 2 == 0)
         amount = amount + mid_odd.get(i);
    }
 
    // Print the amount
    System.out.println(amount);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 2;
   int[][] Arr = {{5, 2, 3, 4}, {1, 6}};
 
   // Function call to calculate the
   // amount of coins collected by A
   find(N, Arr);
}
}
  
// This code is contributed by splevel62.


Python3
# Python Program to implement
# the above approach
 
# Function to calculate the
# maximum amount collected by A
 
 
def find(N, Arr):
 
    # Stores the money
    # obtained by A
    amount = 0
 
    # Stores mid elements
    # of odd sized rows
    mid_odd = []
 
    for i in range(N):
 
        # Size of current row
        siz = len(Arr[i])
 
        # Increase money collected by A
        for j in range(0, siz // 2):
            amount = amount + Arr[i][j]
 
        if(siz % 2 == 1):
            mid_odd.append(Arr[i][siz // 2])
 
    # Add coins at even indices
    # to the amount collected by A
    mid_odd.sort(reverse=True)
 
    for i in range(len(mid_odd)):
        if i % 2 == 0:
            amount = amount + mid_odd[i]
 
    # Print the amount
    print(amount)
 
 
# Driver Code
 
N = 2
Arr = [[5, 2, 3, 4], [1, 6]]
 
# Function call to calculate the
# amount of coins collected by A
find(N, Arr)


C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to calculate the
  // maximum amount collected by A
  static void find(int N, List> Arr)
  {
 
    // Stores the money
    // obtained by A
    int amount = 0;
 
    // Stores mid elements
    // of odd sized rows
    List mid_odd = new List();
    for(int i = 0; i < N; i++)
    {
 
      // Size of current row
      int siz = Arr[i].Count;
 
      // Increase money collected by A
      for (int j = 0; j < siz / 2; j++)
        amount = amount + Arr[i][j];
 
      if(siz % 2 == 1)
        mid_odd.Add(Arr[i][siz/2]);
    }
 
    // Add coins at even indices
    // to the amount collected by A
    mid_odd.Sort();
 
    for(int i = 0; i < mid_odd.Count; i++)
      if (i % 2 == 0)
        amount = amount + mid_odd[i];
 
    // Print the amount
    Console.WriteLine(amount);
 
  }
 
  // Driver code
  static void Main()
  {
    int N = 2;
    List> Arr = new List>();
    Arr.Add(new List());
    Arr[0].Add(5);
    Arr[0].Add(2);
    Arr[0].Add(3);
    Arr[0].Add(4);
    Arr.Add(new List());
    Arr[1].Add(1);
    Arr[1].Add(6);
 
    // Function call to calculate the
    // amount of coins collected by A
    find(N, Arr);
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出:
8

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