Q:

Given a N X N Matrix, Matrix[N][N] of positive integers. There are only three possible moves from a cell Matrix[r][c]

0

Path in Matrix

Given a N X N Matrix, Matrix[N][N] of positive integers. There are only three possible moves from a cell Matrix[r][c].

  1. Matrix[r+1][c]
  2. Matrix[r+1][c-1]
  3. Matrix[r+1][c+1]

Starting from any column in row 0, return the largest sum of any of the paths up to row N-1.

Problem description:

The problem wants you to find the path from any column of first row (0<=col<=N-1) to bottom-right corner of the matrix[N-1][N-1] such that the path you choose provide maximum value of sum during the traversal, the allowed movements are possible in only three direction as right, right-down and down.{(i,j)->(i+1,j+1),(i,j)->(i+1,j) and (i,j)->(i+1,j)}.

Input:

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains a single integer N denoting the order of matrix. Next line contains N*N integers denoting the elements of the matrix in row-major form.

Output:

Output the largest sum of any of the paths starting from any cell of row 0 to any cell of row N-1. Print the output of each test case in a new line.

Examples:

Input:
T=1
N=3
1,2,3
4,5,6
7,8,9

Output:
18,
as moving from 3->6->9.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The problem can be solved with the help of a dynamic programming approach, in this approach we will create a cost 2-D array which will store the cost of visiting that cell.

Since we can move in only three direction from (i,j) that is we can move:

{i+1,j},{i+1,j+1} or {i+1,j-1},in other words we can reach the position {i,j} from three different position as:

{i,j}={i-1,j},{i-1,j+1},{i-1,j-1},we will use this approach and find the max cost for reaching index i,j from above given indices.

Here cost[i][j] is out cost function dp array and we will store input in dp[i][j] array.

So, our final dp state is as follow:

cost[i][j]=dp[i][j]+max(cost[i-1][j],cost[i-1][j+1],cost[i-1][j-1])

We need to keep in mind the index bound range before calculating it.

Since we need to find the maximum cost for reaching the last row, we will iterate through all columns of the last row for maximum value.

Time Complexity for the above approach in the worst case is: O(n*n)

Space Complexity for the above approach in the worst case is: O(n*n)

C++ Implementation:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll dp[27][27];

//solve function.
ll solve(ll n)
{
    //declare cost dp state.
    ll cost[n][n];

    //first row will be same
    //as initial dp since we can
    //start from any column of first row.
    for (ll i = 0; i < n; i++)
        cost[0][i] = dp[0][i];

    //iterate through 2nd row to last row.
    for (ll i = 1; i < n; i++) {
        //iterate through all columns.
        for (ll j = 0; j < n; j++) {

            ll x, y, z;
            x = 0;
            y = 0;
            z = 0;

            //if we are reaching{i,j} from
            //{i-1,j} then check index condition.
            if ((i - 1) >= 0)
                x = cost[i - 1][j];

            //if we are reaching{i,j} from
            //{i-1,j-1} then check index condition.
            if ((i - 1) >= 0 and (j - 1) >= 0)
                y = cost[i - 1][j - 1];

            //if we are reaching{i,j} from
            //{i-1,j+1} then check index condition.
            if ((i - 1) >= 0 and (j + 1) < n)
                z = cost[i - 1][j + 1];

            //finally find the maximum cost.
            cost[i][j] = dp[i][j] + max(max(x, y), z);
        }
    }

    //initialize ans variable with 0.
    ll ans = 0;
    //iterate through last row.
    for (ll i = 0; i < n; i++)
        ans = max(ans, cost[n - 1][i]);

    //return final ans.
    return ans;
}

int main()
{
    ll t;

    cout << "Enter number of test cases: ";
    cin >> t;

    while (t--) {
        cout << "Enter size of matrix: ";
        ll n;
        cin >> n;

        cout << "Enter elements:\n";
        for (ll i = 0; i < n; i++)
            for (ll j = 0; j < n; j++)
                cin >> dp[i][j];

        //call solve function.
        ll ans = solve(n);
        cout << "Maximum Cost: ";
        cout << ans << "\n";
    }
 
    return 0;
}

Output:

Enter number of test cases: 2
Enter size of matrix: 3
Enter elements:
1 2 3
4 5 6
7 8 9
Maximum Cost: 18
Enter size of matrix: 2
Enter elements:
348 391
618 193
Maximum Cost: 1009

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Given an mXn matrix, if an element is 0, set its e... >>
<< You will be given a square matrix M and a positive...