
- 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 points from top left of matrix to bottom right and return back in C++
In this tutorial, we will be discussing a program to find maximum points from top left of matrix to bottom right and return back
For this we will be provided with matrix consisting of #-blocked path, *-points, .- allowed path. Our task is to go from one corner to another (right and below moves) and come back (left and top moves) such as to collect maximum points
Example
#include <bits/stdc++.h> #define MAX 5 #define N 5 #define M 5 #define inf 100000 using namespace std; //calculating points int cost(char grid[][M], int row1, int col1, int row2, int col2) { if (row1 == row2 && col1 == col2) { if (grid[row1][col1] == '*') return 1; return 0; } int ans = 0; if (grid[row1][col1] == '*') ans++; if (grid[row2][col2] == '*') ans++; return ans; } //calculating maximum points int solve(int n, int m, char grid[][M], int dp[MAX][MAX][MAX], int row1, int col1, int row2) { int col2 = (row1 + col1) - (row2); if (row1 == n - 1 && col1 == m - 1 && row2 == n - 1 && col2 == m - 1) return 0; if (row1 >= n || col1 >= m || row2 >= n || col2 >= m) return -1 * inf; if (dp[row1][col1][row2] != -1) return dp[row1][col1][row2]; int ch1 = -1 * inf, ch2 = -1 * inf; int ch3 = -1 * inf, ch4 = -1 * inf; if (grid[row1][col1 + 1] != '#' && grid[row2 + 1][col2] != '#') ch1 = cost(grid, row1, col1 + 1, row2 + 1, col2) + solve(n, m, grid, dp, row1, col1 + 1, row2 + 1); if (grid[row1][col1 + 1] != '#' && grid[row2][col2 + 1] != '#') ch2 = cost(grid, row1, col1 + 1, row2, col2 + 1) + solve(n, m, grid, dp, row1, col1 + 1, row2); if (grid[row1 + 1][col1] != '#' && grid[row2][col2 + 1] != '#') ch3 = cost(grid, row1 + 1, col1, row2, col2 + 1) + solve(n, m, grid, dp, row1 + 1, col1, row2); if (grid[row1 + 1][col1] != '#' && grid[row2 + 1][col2] != '#') ch4 = cost(grid, row1 + 1, col1, row2 + 1, col2) + solve(n, m, grid, dp, row1 + 1, col1, row2 + 1); return dp[row1][col1][row2] = max({ch1, ch2, ch3, ch4}); } int wrapper(int n, int m, char grid[N][M]) { int ans = 0; int dp[MAX][MAX][MAX]; memset(dp, -1, sizeof dp); if (grid[n - 1][m - 1] == '#' || grid[0][0] == '#') ans = -1 * inf; if (grid[0][0] == '*') ans++; grid[0][0] = '.'; if (grid[n - 1][m - 1] == '*') ans++; grid[n - 1][m - 1] = '.'; ans += solve(n, m, grid, dp, 0, 0, 0); return max(ans, 0); } int main() { int n = 5, m = 5; char grid[N][M] = { { '.', '*', '.', '*', '.' }, { '*', '#', '#', '#', '.' }, { '*', '.', '*', '.', '*' }, { '.', '#', '#', '#', '*' }, { '.', '*', '.', '*', '.' } }; cout << wrapper(n, m, grid) << endl; return 0; }
Output
8
- Related Articles
- Maximum sum path in a matrix from top to bottom and back in C++ Program
- Print all palindromic paths from top left to bottom right in a matrix in C++
- Count all possible paths from top left to bottom right of a mXn matrix in C++
- Print all possible paths from top left to bottom right of a mXn matrix in C++
- Maximum sum path in a matrix from top to bottom in C++
- Print all paths from top left to bottom right in a matrix with four moves allowed in C++
- Maximum sum path in a matrix from top to bottom in C++ Program
- How to display the JList items from top to bottom and left to right in Java?
- Set the left, right, top and bottom padding of an element
- Program to count number of walls required to partition top-left and bottom-right cells in Python
- Program to find number of ways we can reach from top left point to bottom right point in Python
- How to add a straight line to a plot in R starting from bottom left and ending at top right?
- A current-carrying conductor is held in exactly vertical direction. In order to produce a clockwise magnetic field around the conductor, the current should passed in the conductor:(a) from top towards bottom (b) from left towards right (c) from bottom towards top (d) from right towards left
- Maximum mirrors which can transfer light from bottom to right in C++
- Program to find number of coins we can pick from topleft to bottom-right cell and return in Python

Advertisements