Related Articles
Minimize cost to reach bottom-right corner of a Matrix using given operations
• Difficulty Level : Hard
• Last Updated : 10 Mar, 2021

Given a matrix grid[][] of size N x N, the task is to find the minimum cost required to reach the bottom right corner of the matrix from the top left corner, where the cost of moving to a new cell is [S/2] + K, where S is the score at the previous index and K is the matrix element at the current index.
Note: Here, [X] is the largest integer which is not exceeding X.

Examples:

Input: grid[][] = {{0, 3, 9, 6}, {1, 4, 4, 5}, {8, 2, 5, 4}, {1, 8, 5, 9}}
Output: 12
Explanation: One of the possible set of moves is as follows 0 -> 1 -> 4 -> 4 -> 7 -> 7 -> 12.

Input: grid[][] = {{0, 82, 2, 6, 7}, {4, 3, 1, 5, 21}, {6, 4, 20, 2, 8}, {6, 6, 64, 1, 8}, {1, 65, 1, 6, 4}}
Output: 7

Approach: Follow the steps below to solve the problem:

• Make the first element of the matrix zero.
• Traverse in the range [0, N-1]:
• Initialize a list, say moveList, and append the moves i and j.
• Initialize another list, say possibleWays, and append moveList in it.
• Initialize a list, say possibleWaysSum, initially as an empty list.
• Traverse the list possibleWays:
• Traverse the appended moveList:
• Check if the move is equal to i, then update i = i + 1.
• Otherwise, update j = j + 1.
• Initialize a variable, say tempSum. Set tempSum = tempSum + grid[i][j].
• Append tempSum in possibleWays list after the loop.
• Print the minimum cost from possibleWaysSum as the output.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `vector > possibleWays;` `// Returns true if str[curr] does not matches with any``// of the characters after str[start]``bool` `shouldSwap(``char` `str[], ``int` `start, ``int` `curr)``{``  ``for` `(``int` `i = start; i < curr; i++) {``    ``if` `(str[i] == str[curr]) {``      ``return` `false``;``    ``}``  ``}``  ``return` `true``;``}` `// Function for the swap``void` `swap(``char` `str[], ``int` `i, ``int` `j)``{``  ``char` `c = str[i];``  ``str[i] = str[j];``  ``str[j] = c;``}` `// Prints all distinct permutations in str[0..n-1]``void` `findPermutations(``char` `str[], ``int` `index, ``int` `n)``{``  ``if` `(index >= n) {``    ``vector<``char``> l;``    ``for` `(``int` `i = 0; i < n; i++)``      ``l.push_back(str[i]);``    ``possibleWays.push_back(l);``    ``return``;``  ``}` `  ``for` `(``int` `i = index; i < n; i++) {` `    ``// Proceed further for str[i] only if it``    ``// doesn't match with any of the characters``    ``// after str[index]``    ``bool` `check = shouldSwap(str, index, i);``    ``if` `(check) {``      ``swap(str, index, i);``      ``findPermutations(str, index + 1, n);``      ``swap(str, index, i);``    ``}``  ``}``}` `// Function to print the minimum cost``void` `minCost(``int` `grid[], ``int` `N)``{` `  ``vector<``char``> moveList;` `  ``// Making top-left value 0``  ``// implicitly to generate list of moves``  ``grid = 0;``  ``vector<``int``> possibleWaysSum;` `  ``for` `(``int` `k = 0; k < N - 1; k++) {` `    ``moveList.push_back(``'i'``);``    ``moveList.push_back(``'j'``);` `    ``possibleWays.clear();` `    ``// Convert into set to make only unique values``    ``int` `n = moveList.size();``    ``char` `str[n];``    ``for` `(``int` `i = 0; i < n; i++)``      ``str[i] = moveList[i];` `    ``// To store the unique permutation of moveLst``    ``// into the possibleWays``    ``findPermutations(str, 0, n);``    ``possibleWaysSum.clear();` `    ``// Traverse the list``    ``for` `(vector<``char``> way : possibleWays) {``      ``int` `i = 0, j = 0, tempSum = 0;``      ``for` `(``char` `move : way) {``        ``if` `(move == ``'i'``) {``          ``i += 1;``        ``}``        ``else` `{``          ``j += 1;``        ``}` `        ``// Stores cost according to given``        ``// conditions``        ``tempSum = (``int``)(``floor``(tempSum / 2))``          ``+ grid[i][j];``      ``}``      ``possibleWaysSum.push_back(tempSum);``    ``}``  ``}` `  ``// Print the minimum possible cost``  ``int` `ans = possibleWaysSum;``  ``for` `(``int` `i = 1; i < possibleWaysSum.size(); i++)``    ``ans = min(ans, possibleWaysSum[i]);` `  ``cout << ans;``}` `// Driven Program``int` `main()``{``  ` `  ``// Size of the grid``  ``int` `N = 4;` `  ``// Given grid[][]``  ``int` `grid[] = { { 0, 3, 9, 6 },``                   ``{ 1, 4, 4, 5 },``                   ``{ 8, 2, 5, 4 },``                   ``{ 1, 8, 5, 9 } };` `  ``// Function call to print the minimum``  ``// cost to reach bottom-right corner``  ``// from the top-left corner of the matrix``  ``minCost(grid, N);` `  ``return` `0;``}` `// This code is contributed by Kingash.`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `class` `GFG``{``    ``static` `ArrayList > possibleWays;` `    ``// Returns true if str[curr] does not matches with any``    ``// of the characters after str[start]``    ``static` `boolean` `shouldSwap(``char` `str[], ``int` `start,``                              ``int` `curr)``    ``{``        ``for` `(``int` `i = start; i < curr; i++)``        ``{``            ``if` `(str[i] == str[curr])``            ``{``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}` `    ``// Prints all distinct permutations in str[0..n-1]``    ``static` `void` `findPermutations(``char` `str[], ``int` `index,``                                 ``int` `n)``    ``{``        ``if` `(index >= n)``        ``{``            ``ArrayList l = ``new` `ArrayList<>();``            ``for` `(``char` `ch : str)``                ``l.add(ch);``            ``possibleWays.add(l);``            ``return``;``        ``}` `        ``for` `(``int` `i = index; i < n; i++) {` `            ``// Proceed further for str[i] only if it``            ``// doesn't match with any of the characters``            ``// after str[index]``            ``boolean` `check = shouldSwap(str, index, i);``            ``if` `(check) {``                ``swap(str, index, i);``                ``findPermutations(str, index + ``1``, n);``                ``swap(str, index, i);``            ``}``        ``}``    ``}` `    ``// Function for the swap``    ``static` `void` `swap(``char``[] str, ``int` `i, ``int` `j)``    ``{``        ``char` `c = str[i];``        ``str[i] = str[j];``        ``str[j] = c;``    ``}` `    ``// Function to print the minimum cost``    ``static` `void` `minCost(``int` `grid[][], ``int` `N)``    ``{` `        ``ArrayList moveList = ``new` `ArrayList<>();` `        ``// Making top-left value 0``        ``// implicitly to generate list of moves``        ``grid[``0``][``0``] = ``0``;``        ``ArrayList possibleWaysSum``            ``= ``new` `ArrayList<>();` `        ``for` `(``int` `k = ``0``; k < N - ``1``; k++) {` `            ``moveList.add(``'i'``);``            ``moveList.add(``'j'``);` `            ``possibleWays = ``new` `ArrayList<>();` `            ``// Convert into set to make only unique values``            ``int` `n = moveList.size();``            ``char` `str[] = ``new` `char``[n];``            ``for` `(``int` `i = ``0``; i < n; i++)``                ``str[i] = moveList.get(i);` `            ``// To store the unique permutation of moveLst``            ``// into the possibleWays``            ``findPermutations(str, ``0``, n);``            ``possibleWaysSum = ``new` `ArrayList<>();` `            ``// Traverse the list``            ``for` `(ArrayList way : possibleWays) {``                ``int` `i = ``0``, j = ``0``, tempSum = ``0``;``                ``for` `(``char` `move : way) {``                    ``if` `(move == ``'i'``) {``                        ``i += ``1``;``                    ``}``                    ``else` `{``                        ``j += ``1``;``                    ``}``                  ` `                    ``// Stores cost according to given``                    ``// conditions``                    ``tempSum = (``int``)(Math.floor(tempSum / ``2``))``                              ``+ grid[i][j];``                ``}``                ``possibleWaysSum.add(tempSum);``            ``}``        ``}` `        ``// Print the minimum possible cost``        ``int` `ans = possibleWaysSum.get(``0``);``        ``for` `(``int` `i = ``1``; i < possibleWaysSum.size(); i++)``            ``ans = Math.min(ans, possibleWaysSum.get(i));` `        ``System.out.println(ans);``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// Size of the grid``        ``int` `N = ``4``;` `        ``// Given grid[][]``        ``int` `grid[][] = { { ``0``, ``3``, ``9``, ``6` `},``                         ``{ ``1``, ``4``, ``4``, ``5` `},``                         ``{ ``8``, ``2``, ``5``, ``4` `},``                         ``{ ``1``, ``8``, ``5``, ``9` `} };` `        ``// Function call to print the minimum``        ``// cost to reach bottom-right corner``        ``// from the top-left corner of the matrix``        ``minCost(grid, N);``    ``}``}` `// This code is contributed by Kingash.`

## Python3

 `# Python3 program for the above approach` `from` `itertools ``import` `permutations``from` `math ``import` `floor` `# Function to print the minimum cost`  `def` `minCost(grid, N):``    ``moveList ``=` `[]` `    ``# Making top-left value 0``    ``# implicitly to generate list of moves``    ``grid[``0``][``0``] ``=` `0` `    ``for` `i ``in` `range``(N ``-` `1``):``        ``moveList.append(``'i'``)``        ``moveList.append(``'j'``)` `        ``# Convert into set to make only unique values``        ``possibleWays ``=` `list``(``set``(permutations(moveList)))``        ``possibleWaysSum ``=` `[]` `        ``# Traverse the list``        ``for` `way ``in` `possibleWays:``            ``i, j, tempSum ``=` `0``, ``0``, ``0``            ``for` `move ``in` `way:``                ``if` `move ``=``=` `'i'``:``                    ``i ``+``=` `1``                ``else``:``                    ``j ``+``=` `1` `                ``# Stores cost according to given conditions``                ``tempSum ``=` `floor(tempSum``/``2``) ``+` `grid[i][j]` `            ``possibleWaysSum.append(tempSum)``        ``minWayIndex ``=` `possibleWaysSum.index(``min``(possibleWaysSum))` `    ``# Print the minimum possible ccost``    ``print``(``min``(possibleWaysSum))`  `# Size of the grid``N ``=` `4` `# Given grid[][]``grid ``=` `[[``0``, ``3``, ``9``, ``6``], [``1``, ``4``, ``4``, ``5``], [``8``, ``2``, ``5``, ``4``], [``1``, ``8``, ``5``, ``9``]]` `# Function call to print the minimum``# cost to reach bottom-right corner``# from the top-left corner of the matrix``minCost(grid, N)`
Output:
`12`

Time Complexity: O(N2)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up