- Related Questions & Answers
- C Program for Tower of Hanoi
- C++ Program for Dijkstra’s shortest path algorithm?
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C program to find the length of a string?
- C Program to Check if all digits of a number divide it
- C Program to Check if a Given String is a Palindrome?
- Write a program to Delete a Tree in C programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Sum of the nodes of a Singly Linked List in C Program
- Sum of first n natural numbers in C Program
- C Program for simple interest?
- Area of a n-sided regular polygon with given Radius in C Program?
- Area of a leaf inside a square in C Program?
- C Program for Area of a square inscribed in a circle which is inscribed in a hexagon?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will learn about the solution to the problem statement given below.

**Problem statement** − We are given a cost matrix and a position (m, n), we need to find the cost of minimum cost path to reach (m, n) from (0, 0). Each cell represents a cost to traverse from one cell to another.

Now let’s observe the solution in the implementation below −

# dynamic approach R = 3 C = 3 def minCost(cost, m, n): # initialization tc = [[0 for x in range(C)] for x in range(R)] # base case tc[0][0] = cost[0][0] # total cost(tc) array for i in range(1, m + 1): tc[i][0] = tc[i-1][0] + cost[i][0] # tc array for j in range(1, n + 1): tc[0][j] = tc[0][j-1] + cost[0][j] # rest tc array for i in range(1, m + 1): for j in range(1, n + 1): tc[i][j] = min(tc[i-1][j-1], tc[i-1][j], tc[i][j-1]) + cost[i][j] return tc[m][n] # main cost = [[1, 5, 3], [7, 7, 4], [8, 5, 3]] print("Total Cost:",minCost(cost, 2, 1))

Total Cost: 13

All the variables are declared in the local scope and their references are seen in the figure above.

In this article, we have learned about how we can make a Python Program for Min Cost Path

Advertisements