Maximum power in N levels from K such that defeating boss at level A[i] increases power by B[i] using C++


In the realm of game development, optimizing player power and progression is a crucial aspect of creating engaging and challenging experiences. One common mechanic involves defeating bosses at various levels, with each victory granting the player an increase in power. In this article, we will explore how to calculate the maximum power a player can achieve in N levels from a given initial power level, K, while considering the power increment, B[i], gained by defeating a boss at level A[i]. We will delve into the syntax, algorithm, and present two distinct approaches with full executable code examples in C++.

Syntax

Before exploring this topic further. It is essential that we outline and clarify the syntax involved in using the selected method for our forthcoming code illustrations. Having established this foundation we can then move forward with a more comprehensive understanding of this particular technique. −

int calculateMaximumPower(int N, int K, int A[], int B[]);

Algorithm

To determine the maximum power attainable in N levels, we can follow the following step-by-step algorithm −

  • Initialize a variable, maxPower, to store the maximum power obtained.

  • Set a variable, currentPower, to the initial power level, K.

  • Iterate over each level, i, from 0 to N-1 −

  • If defeating the boss at level A[i] results in a power increment of B[i], update currentPower by adding B[i].

  • Check if currentPower is greater than maxPower. If so, update maxPower with the new value.

  • Return maxPower as the maximum attainable power in N levels.

Approach 1: Dynamic Programming

A viable solution to tackle this issue involves utilizing dynamic programming. To effectively store the maximum achievable power at each level, initialize an array called dp, having a size of N+1.

Example

#include <iostream>
#include <algorithm>

int calculateMaximumPower(int N, int K, int A[], int B[]) {
   int dp[N + 1];
   dp[0] = K;

   for (int i = 1; i <= N; i++) {
      dp[i] = dp[i - 1];
      for (int j = 0; j < i; j++) {
         if (A[j] <= i)
            dp[i] = std::max(dp[i], dp[i - A[j]] + B[j]);
      }
   }

   return dp[N];
}

int main() {
   // Example usage
   int N = 5;
   int K = 10;
   int A[] = {2, 3, 1, 4, 2};
   int B[] = {5, 3, 2, 7, 4};

   int maxPower = calculateMaximumPower(N, K, A, B);
   
   std::cout << "Maximum power achievable: " << maxPower << std::endl;

   return 0;
}

Output

Maximum power achievable: 22

Explanation

In this approach, we utilize dynamic programming to calculate the maximum power achievable in N levels. We create an array, dp, of size N+1 to store the maximum power attainable at each level. At outset, dp[0], our dynamic programming array starts with a value of K which signifies the initial power level. Moving ahead, our approach for every i-th level from 1 all through N involves updating this array thus: We retrieve and store into memory ,the greatest attainable power after getting victorious over captains in earlier levels..It is important to note that defeating a boss situated at location A [j],correctly leads to an increase in one's strength by B [j](where j spans between values 0 all through i-1). By using max(dp[i - A[j]] + B [j],dp [i]).we are able to update the value of dp[i] so that its previous maximum strength becomes as reflected by the current result. Finally, we return dp[N] as the maximum attainable power in N levels. This approach has a time complexity of O(N^2) due to the nested loop.

Approach 2: Using greedy algorithm

Using a greedy algorithm may offer an effective solution. This entails sorting levels by increasing boss level, A[i], followed by iterating through each stage of play and boosting power only when it contributes towards defeating that particular boss and thus practicing good decision-making.

Example

#include <iostream>
#include <algorithm>

bool compareLevels(std::pair<int, int> boss1, std::pair<int, int> boss2) {
   return boss1.first < boss2.first;
}

int calculateMaximumPower(int N, int K, int A[], int B[]) {
   std::pair<int, int> bosses[N];
   for (int i = 0; i < N; i++) {
      bosses[i] = std::make_pair(A[i], B[i]);
   }

   std::sort(bosses, bosses + N, compareLevels);

   int currentPower = K;
   int maxPower = K;
   int index = 0;

   for (int i = 1; i <= N; i++) {
      while (index < N && bosses[index].first <= i) {
         currentPower += bosses[index].second;
         index++;
      }

      maxPower = std::max(maxPower, currentPower);
   }
   return maxPower;
}

int main() {
   // Example usage
   int N = 5;
   int K = 10;
   int A[] = {2, 3, 1, 4, 2};
   int B[] = {5, 3, 2, 7, 4};

   int maxPower = calculateMaximumPower(N, K, A, B);

   std::cout << "Maximum power achievable: " << maxPower << std::endl;

   return 0;
}

Output

Maximum power achievable: 31

Explanation

In the greedy algorithm approach, we first sort the levels based on the boss level, A[i], in ascending order. We then iterate through each level from 1 to N. We maintain a currentPower variable to track the current power level and a maxPower variable to store the maximum power achieved so far. Starting with the initial power level, K, we check if defeating the boss at the current level increases the power. If so, we update currentPower by adding the power increment, B[i]. We continue this process until all bosses up to the current level are defeated. We update maxPower whenever currentPower surpasses it. By the end of the iteration, maxPower will contain the maximum attainable power in N levels. This approach has a time complexity of O(N log N) due to the sorting operation.

Conclusion

Our article addresses how to ascertain the peak power attainable over N tiers - beginning from an original energy level K when incremental energy bonuses are gained after vanquishing specific stage bosses. We present two options: using dynamic programming or employing a greedy algorithm.

Although both methods deliver viable results, there are minor divergences between them regarding implementation. Game developers who learn these skills and incorporate them through C++ programming will construct satisfying progression systems that engage users in captivating gameplay experiences replete with rich rewards.

Updated on: 25-Jul-2023

25 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements