Consider a game, in which you have two types of powers, A and B and there are 3 types of Areas X, Y and Z. Every second you have to switch between these areas, and each area has specific properties by which your power A and power B increase or decrease. We need to keep choosing areas in such a way that our survival time is maximized. Survival time ends when any of the powers, A or B reaches less than 0.Â
Examples:Â
Initial value of Power A = 20
Initial value of Power B = 8
Area X (3, 2) : If you step into Area X,
A increases by 3,
B increases by 2
Area Y (-5, -10) : If you step into Area Y,
A decreases by 5,
B decreases by 10
Area Z (-20, 5) : If you step into Area Z,
A decreases by 20,
B increases by 5
It is possible to choose any area in our first step.
We can survive at max 5 unit of time by following
these choice of areas :
X -> Z -> X -> Y -> X
This problem can be solved using recursion, after each time unit we can go to any of the area but we will choose that area which ultimately leads to maximum survival time. As recursion can lead to solving same subproblem many time, we will memoize the result on basis of power A and B, if we reach to same pair of power A and B, we won’t solve it again instead we will take the previously calculated result.Â
Given below is the simple implementation of above approach.Â
CPP
//Â C++ code to get maximum survival time#include <bits/stdc++.h>using namespace std;Â
//Â structure to represent an areastruct area{Â Â Â Â //Â increment or decrement in A and BÂ Â Â Â int a, b;Â Â Â Â area(int a, int b) : a(a), b(b)Â Â Â Â {}};Â
//Â Utility method to get maximum of 3 integersint max(int a, int b, int c){Â Â Â Â return max(a, max(b, c));}Â
// Utility method to get maximum survival timeint maxSurvival(int A, int B, area X, area Y, area Z,                int last, map<pair<int, int>, int>& memo){    // if any of A or B is less than 0, return 0    if (A <= 0 || B <= 0)        return 0;    pair<int, int> cur = make_pair(A, B);Â
    // if already calculated, return calculated value    if (memo.find(cur) != memo.end())        return memo[cur];Â
    int temp;Â
    // step to areas on basis of last choose area    switch(last)    {    case 1:        temp = 1 + max(maxSurvival(A + Y.a, B + Y.b,                                   X, Y, Z, 2, memo),                       maxSurvival(A + Z.a, B + Z.b,                                  X, Y, Z, 3, memo));        break;    case 2:        temp = 1 + max(maxSurvival(A + X.a, B + X.b,                                  X, Y, Z, 1, memo),                       maxSurvival(A + Z.a, B + Z.b,                                  X, Y, Z, 3, memo));        break;    case 3:        temp = 1 + max(maxSurvival(A + X.a, B + X.b,                                  X, Y, Z, 1, memo),                       maxSurvival(A + Y.a, B + Y.b,                                  X, Y, Z, 2, memo));        break;    }Â
    // store the result into map    memo[cur] = temp;Â
    return temp;}Â
//Â method returns maximum survival timeint getMaxSurvivalTime(int A, int B, area X, area Y, area Z){Â Â Â Â if (A <= 0 || B <= 0)Â Â Â Â Â Â Â Â return 0;Â Â Â Â map< pair<int, int>, int > memo;Â
    // At first, we can step into any of the area    return        max(maxSurvival(A + X.a, B + X.b, X, Y, Z, 1, memo),            maxSurvival(A + Y.a, B + Y.b, X, Y, Z, 2, memo),            maxSurvival(A + Z.a, B + Z.b, X, Y, Z, 3, memo));}Â
//Â Driver code to test above methodint main(){Â Â Â Â area X(3, 2);Â Â Â Â area Y(-5, -10);Â Â Â Â area Z(-20, 5);Â
    int A = 20;    int B = 8;    cout << getMaxSurvivalTime(A, B, X, Y, Z);Â
    return 0;} |
Java
/*package whatever //do not write package name here */import java.util.*;import java.io.*;Â
class GFG {Â Â // Java code to get maximum survival timeÂ
  // class to represent an area  static class area  {    // increment or decrement in A and B    public int a, b;    public area(int a, int b){      this.a = a;      this.b = b;    }  };Â
  // class to represent pair  static class Pair{    public int first,second;    public Pair(int first,int second){      this.first = first;      this.second = second;    }  }Â
Â
  // Utility method to get maximum of 3 integers  static int max(int a, int b, int c)  {    return Math.max(a, Math.max(b, c));  }Â
  // Utility method to get maximum survival time  static int maxSurvival(int A, int B, area X, area Y, area Z,                         int last, HashMap<Pair, Integer> memo)  {    // if any of A or B is less than 0, return 0    if (A <= 0 || B <= 0)      return 0;    Pair cur = new Pair(A, B);Â
    // if already calculated, return calculated value    if (memo.containsKey(cur))      return memo.get(cur);Â
    int temp = 0;Â
    // step to areas on basis of last choose area    switch(last)    {      case 1:        temp = 1 + Math.max(maxSurvival(A + Y.a, B + Y.b,                                        X, Y, Z, 2, memo),                            maxSurvival(A + Z.a, B + Z.b,                                        X, Y, Z, 3, memo));        break;      case 2:        temp = 1 + Math.max(maxSurvival(A + X.a, B + X.b,                                        X, Y, Z, 1, memo),                            maxSurvival(A + Z.a, B + Z.b,                                        X, Y, Z, 3, memo));        break;      case 3:        temp = 1 + Math.max(maxSurvival(A + X.a, B + X.b,                                        X, Y, Z, 1, memo),                            maxSurvival(A + Y.a, B + Y.b,                                        X, Y, Z, 2, memo));        break;    }Â
    // store the result into map    memo.put(cur,temp);Â
    return temp;  }Â
  // method returns maximum survival time  static int getMaxSurvivalTime(int A, int B, area X, area Y, area Z)  {    if (A <= 0 || B <= 0)      return 0;    HashMap<Pair,Integer> memo = new HashMap<>();Â
    // At first, we can step into any of the area    return      max(maxSurvival(A + X.a, B + X.b, X, Y, Z, 1, memo),          maxSurvival(A + Y.a, B + Y.b, X, Y, Z, 2, memo),          maxSurvival(A + Z.a, B + Z.b, X, Y, Z, 3, memo));  }Â
  // Driver Code  public static void main(String args[])  {    area X = new area(3, 2);    area Y = new area(-5, -10);    area Z = new area(-20, 5);Â
    int A = 20;    int B = 8;    System.out.println(getMaxSurvivalTime(A, B, X, Y, Z));  }}Â
// This code is contributed by shinjanpatra |
Python3
# Python code to get maximum survival timeÂ
# Class to represent an areaÂ
Â
class area:    def __init__(self, a, b):        self.a = a        self.b = bÂ
# Utility method to get maximum survival timeÂ
Â
def maxSurvival(A, B, X, Y, Z, last, memo):Â Â Â Â # if any of A or B is less than 0, return 0Â Â Â Â if (A <= 0 or B <= 0):Â Â Â Â Â Â Â Â return 0Â Â Â Â cur = area(A, B)Â
    # if already calculated, return calculated value    for ele in memo.keys():        if (cur.a == ele.a and cur.b == ele.b):            return memo[ele]Â
    # step to areas on basis of last chosen area    if (last == 1):        temp = 1 + max(maxSurvival(A + Y.a, B + Y.b,                                   X, Y, Z, 2, memo),                       maxSurvival(A + Z.a, B + Z.b,                                   X, Y, Z, 3, memo))    elif (last == 2):        temp = 1 + max(maxSurvival(A + X.a, B + X.b,                                   X, Y, Z, 1, memo),                       maxSurvival(A + Z.a, B + Z.b,                                   X, Y, Z, 3, memo))    elif (last == 3):        temp = 1 + max(maxSurvival(A + X.a, B + X.b,                                   X, Y, Z, 1, memo),                       maxSurvival(A + Y.a, B + Y.b,                                   X, Y, Z, 2, memo))Â
    # store the result into map    memo[cur] = tempÂ
    return tempÂ
# method returns maximum survival timeÂ
Â
def getMaxSurvivalTime(A, B, X, Y, Z):Â Â Â Â if (A <= 0 or B <= 0):Â Â Â Â Â Â Â Â return 0Â Â Â Â memo = dict()Â
    # At first, we can step into any of the area    return max(maxSurvival(A + X.a, B + X.b, X, Y, Z, 1, memo),               maxSurvival(A + Y.a, B + Y.b, X, Y, Z, 2, memo),               maxSurvival(A + Z.a, B + Z.b, X, Y, Z, 3, memo))Â
Â
# Driver code to test above methodX = area(3, 2)Y = area(-5, -10)Z = area(-20, 5)Â
A = 20B = 8print(getMaxSurvivalTime(A, B, X, Y, Z))Â
# This code is contributed by Soumen Ghosh. |
C#
// C# code to get maximum survival timeusing System;using System.Collections.Generic;Â
class GFG {Â
  // class to represent an area  class area {    // increment or decrement in A and B    public int a, b;    public area(int a, int b)    {      this.a = a;      this.b = b;    }  };Â
  // class to represent pair  class Pair {    public int first, second;    public Pair(int first, int second)    {      this.first = first;      this.second = second;    }  }Â
  // Utility method to get maximum of 3 integers  static int max(int a, int b, int c)  {    return Math.Max(a, Math.Max(b, c));  }Â
  // Utility method to get maximum survival time  static int maxSurvival(int A, int B, area X, area Y,                         area Z, int last,                         Dictionary<Pair, int> memo)  {    // if any of A or B is less than 0, return 0    if (A <= 0 || B <= 0)      return 0;    Pair cur = new Pair(A, B);Â
    // if already calculated, return calculated value    if (memo.ContainsKey(cur))      return memo[cur];Â
    int temp = 0;Â
    // step to areas on basis of last choose area    switch (last) {      case 1:        temp          = 1          + Math.Max(maxSurvival(A + Y.a, B + Y.b,                                 X, Y, Z, 2, memo),                     maxSurvival(A + Z.a, B + Z.b,                                 X, Y, Z, 3, memo));        break;      case 2:        temp          = 1          + Math.Max(maxSurvival(A + X.a, B + X.b,                                 X, Y, Z, 1, memo),                     maxSurvival(A + Z.a, B + Z.b,                                 X, Y, Z, 3, memo));        break;      case 3:        temp          = 1          + Math.Max(maxSurvival(A + X.a, B + X.b,                                 X, Y, Z, 1, memo),                     maxSurvival(A + Y.a, B + Y.b,                                 X, Y, Z, 2, memo));        break;    }Â
    // store the result into map    memo[cur] = temp;Â
    return temp;  }Â
  // method returns maximum survival time  static int getMaxSurvivalTime(int A, int B, area X,                                area Y, area Z)  {    if (A <= 0 || B <= 0)      return 0;    Dictionary<Pair, int> memo      = new Dictionary<Pair, int>();Â
    // At first, we can step into any of the area    return max(      maxSurvival(A + X.a, B + X.b, X, Y, Z, 1, memo),      maxSurvival(A + Y.a, B + Y.b, X, Y, Z, 2, memo),      maxSurvival(A + Z.a, B + Z.b, X, Y, Z, 3,                  memo));  }Â
  // Driver Code  public static void Main(String[] args)  {    area X = new area(3, 2);    area Y = new area(-5, -10);    area Z = new area(-20, 5);Â
    int A = 20;    int B = 8;    Console.WriteLine(      getMaxSurvivalTime(A, B, X, Y, Z));  }}Â
// This code is contributed by lokeshpotta20. |
Javascript
<script>Â
// JavaScript code to get maximum survival timeÂ
// Class to represent an areaclass area{    constructor(a, b){        this.a = a        this.b = b    }}Â
// Utility method to get maximum survival timefunction maxSurvival(A, B, X, Y, Z, last, memo){Â Â Â Â // if any of A or B is less than 0, return 0Â Â Â Â if (A <= 0 || B <= 0)Â Â Â Â Â Â Â Â return 0Â Â Â Â let cur = new area(A, B)Â
    // if already calculated, return calculated value    for(let [key,value] of memo){        if (cur.a == key.a && cur.b == key.b)            return memo.get(key)    }Â
    let temp;Â
    // step to areas on basis of last chosen area    if (last == 1){        temp = 1 + Math.max(maxSurvival(A + Y.a, B + Y.b,                                   X, Y, Z, 2, memo),                       maxSurvival(A + Z.a, B + Z.b,                                   X, Y, Z, 3, memo))    }    else if (last == 2){        temp = 1 + Math.max(maxSurvival(A + X.a, B + X.b,                                   X, Y, Z, 1, memo),               maxSurvival(A + Z.a, B + Z.b,                   X, Y, Z, 3, memo))    }    else if (last == 3){        temp = 1 + Math.max(maxSurvival(A + X.a, B + X.b,                   X, Y, Z, 1, memo),               maxSurvival(A + Y.a, B + Y.b,                   X, Y, Z, 2, memo))    }Â
    // store the result into map    memo.set(cur , temp)Â
    return temp}Â
// method returns maximum survival timefunction getMaxSurvivalTime(A, B, X, Y, Z){Â Â Â Â if (A <= 0 || B <= 0)Â Â Â Â Â Â Â Â return 0Â Â Â Â let memo = new Map()Â
    // At first, we can step into any of the area    return Math.max(maxSurvival(A + X.a, B + X.b, X, Y, Z, 1, memo),           maxSurvival(A + Y.a, B + Y.b, X, Y, Z, 2, memo),           maxSurvival(A + Z.a, B + Z.b, X, Y, Z, 3, memo))}Â
// Driver code to test above methodlet X = new area(3, 2)let Y = new area(-5, -10)let Z = new area(-20, 5)Â
let A = 20let B = 8document.write(getMaxSurvivalTime(A, B, X, Y, Z),"</br>")Â
   // This code is contributed by shinjanpatraÂ
</script> |
5
This article is contributed by Utkarsh Trivedi. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
