Number of Paths with Max Score in C++


Suppose we have a square board of characters. We can move on the board starting at the bottom right square marked with the character 'S'. Now we need to reach the top left square marked with the character 'E'. The other squares are labeled either with a numeric character from 1 to 9 or with an obstacle 'X'. In one move we can go up, left or up-left only when there is no obstacle there.

We have to find the list of two numbers: the first number is the maximum sum of the numeric characters we can collect, and the second one is the number of such paths that we can take to get that maximum sum. For the answer return it modulo 10^9 + 7. When there is no path, then return [0, 0].

So, if the input is like board = ["E12","1X1","21S"], then the output will be [1,2]

To solve this, we will follow these steps −

  • n := number of rows, m := number of columns

  • Define one 3D array of order n x m x 2

  • dp[n - 1, m - 1, 0] = 0, dp[n - 1, m - 1, 1] = 1

  • for initialize i := m - 2, when i >= 0, update (decrease i by 1), do −

    • if b[n - 1, i] is same as ASCII of 'X', then −

      • dp[n - 1, i, 0] = b[n - 1, i] - ASCII of '0' + dp[n - 1, i + 1, 0]

    • dp[n - 1, i, 1] + = dp[n - 1, i + 1, 1]

  • for initialize i := n - 2, when i >= 0, update (decrease i by 1), do −

    • if b[i, m - 1] is same as ASCII of 'X', then −

      • dp[i, m - 1, 0] = b[i, m - 1] - ASCII of '0' + dp[i + 1, m - 1, 0]

    • dp[i, m - 1, 1] + = dp[i + 1, m - 1, 1]

  • for initialize i := n - 2, when i >= 0, update (decrease i by 1), do −

    • for initialize j := m - 2, when j >= 0, update (decrease j by 1), do −

      • if b[i, j] is same as ASCII of 'X', then −

        • dp[i, j, 0] := (if b[i, j] is same as ASCII of 'E', then 0, otherwise b[i, j] - ASCII of '0')

      • maxVal := maximum of {dp[i, j + 1, 0], dp[i + 1, j, 0], dp[i + 1, j + 1, 0]}

      • if maxVal is same as 0 and (b[i + 1, j] is not equal to ASCII of 'S' and b[i, j + 1] is not equal to ASCII of 'S' and b[i + 1, j + 1] is not equal to ASCII of 'S'), then −

        • dp[i, j, 0] := 0

        • Ignore following part, skip to the next iteration

      • dp[i, j, 0] := dp[i, j, 0] + maxVal

      • if dp[i + 1, j, 0] is same as maxVal, then −

        • dp[i, j, 1] := dp[i, j, 1] + dp[i + 1, j, 1]

      • if dp[i + 1, j + 1, 0] is same as maxVal, then −

        • dp[i, j, 1] := dp[i, j, 1] + dp[i + 1, j + 1, 1]

      • if dp[i, j + 1, 0] is same as maxVal, then −

        • dp[i, j, 1] := dp[i, j, 1] + dp[i, j + 1, 1]

      • dp[i, j, 1] := dp[i, j, 1] mod m

      • dp[i, j, 0] := dp[i, j, 0] mod m

  • return dp[0, 0]

Let us see the following implementation to get better understanding −

Example

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   } cout << "]"<<endl;
}
typedef long long int lli;
const lli m = 1e9 + 7;
lli add(lli a, lli b){
   return ((a % m) + (b % m) % m);
} class Solution {
   public:
   vector<int> pathsWithMaxScore(vector<string>& b) {
      int n = b.size();
      int m = b[0].size();
      vector < vector < vector <int> > > dp(n, vector < vector
      <int> >(m, vector <int> (2)));
      dp[n - 1][m - 1][0] = 0;
      dp[n - 1][m - 1][1] = 1;
      for(int i = m - 2; i >= 0; i--){
         if(b[n - 1][i] == 'X')break;
         dp[n - 1][i][0] = b[n - 1][i] - '0' + dp[n - 1][i + 1]
         [0];
         dp[n - 1][i][1] += dp[n - 1][i + 1][1];
      }
      for(int i = n - 2; i >= 0; i--){
         if(b[i][m - 1] == 'X')break;
         dp[i][m - 1][0] = b[i][m - 1] - '0' + dp[i + 1][m - 1]
         [0];
         dp[i][m - 1][1] += dp[i + 1][m - 1][1];
      }
      for(int i = n - 2; i >= 0; i--){
         for(int j = m - 2; j >= 0; j--){
            if(b[i][j] == 'X')continue;
            dp[i][j][0] = b[i][j] == 'E' ? 0 :b[i][j] - '0';
            int maxVal = max({dp[i][j + 1][0], dp[i + 1][j][0],
            dp[i + 1][j + 1][0]});
            if(maxVal == 0 && (b[i+1][j] != 'S' && b[i][j + 1] !
            = 'S' && b[i+1][j + 1] != 'S')){
               dp[i][j][0] = 0;
               continue;
            }
            dp[i][j][0] += maxVal;
            if(dp[i + 1][j][0] == maxVal){
               dp[i][j][1] += dp[i + 1][j][1];
            }
            if(dp[i + 1][j + 1][0] == maxVal){
               dp[i][j][1] += dp[i + 1][j + 1][1];
            }
            if(dp[i][j + 1][0] == maxVal){
               dp[i][j][1] += dp[i][j + 1][1];
            }
            dp[i][j][1] %= m;
            dp[i][j][0] %= m;
         }
      }
      return dp[0][0];
   }
};
main(){
   Solution ob;
   vector<string> v = {"E12","1X1","21S"};
   print_vector(ob.pathsWithMaxScore(v));
}

Input

{"E12","1X1","21S"}

Output

[1, 2, ]

Updated on: 08-Jun-2020

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements