
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row in C++
In this tutorial, we will be discussing a program to find maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
For this we will be provided with a matrix with possible moves of (i+1, j), (i+1, j-1), (i+1, j+1). Our task is to start from zeroth position and move to last row finding out the maximum sum path.
Example
#include<bits/stdc++.h> using namespace std; #define N 4 //finding maximum sum path int MaximumPath(int Mat[][N]) { int result = 0 ; int dp[N][N+2]; memset(dp, 0, sizeof(dp)); for (int i = 0 ; i < N ; i++) dp[0][i+1] = Mat[0][i]; for (int i = 1 ; i < N ; i++) for (int j = 1 ; j <= N ; j++) dp[i][j] = max(dp[i-1][j-1], max(dp[i-1][j], dp[i-1][j+1])) + Mat[i][j-1] ; for (int i=0; i<=N; i++) result = max(result, dp[N-1][i]); return result ; } int main() { int Mat[4][4] = { { 4, 2 , 3 , 4 }, { 2 , 9 , 1 , 10}, { 15, 1 , 3 , 0 }, { 16 ,92, 41, 44 } }; cout << MaximumPath ( Mat ) <<endl ; return 0; }
Output
120
- Related Articles
- Maximum weight path ending at any element of last row in a matrix in C++
- Find sum of Series with n-th term as n^2 - (n-1)^2 in C++
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- Python – Extract Row with any Boolean True
- Find row with maximum sum in a Matrix in C++
- C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Check if sums of i-th row and i-th column are same in matrix in Python
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- If $(m + 1)$th term of an A.P. is twice the $(n + 1)$th term, prove that $(3m + 1)$th term is twice the $(m + n + 1)$th term.
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Find if there is any subset of size K with 0 sum in an array of -1 and +1 in C++
- C++ program to remove Nodes that Don't Lie in Any Path With Sum>=k
- If $m^{th}$ term of an $A.P.$ is $\frac{1}{n}$ and $n^{th}$ term of another $A.P.$ is $\frac{1}{m}$. Then, show that $(mn)^{th}$ term is equal to $1$.
- Find the row with maximum number of 1s using C++

Advertisements