Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Minimum Cost Path #1423

Merged
merged 1 commit into from May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions Minimum_Cost_Path/Minimum_Cost_Path.c
@@ -0,0 +1,67 @@
/*
Finding minimum cost path in 2-D array "array[][]" to reach a position (left, right)
in array[][] from (0, 0).
Total cost of a path to reach (left, right) is sum of all the costs on that
path (including both source and destination).
*/

#include<stdio.h>
#include<limits.h>

// Number of rows and columns
int row, col;

// Finding minimum cost path in 2-D array
int minimumCost(int array[row][col], int left, int right)
{
int min;

// For invalid left and right query
if (left < 0 || right < 0)
return INT_MAX;
else if (left == 0 && right == 0)
return array[left][right];
else
{
// Finding path with minimum cost i.e. which way to move down, right and diagonally lower cells
if (minimumCost(array, left - 1, right - 1) < minimumCost(array, left - 1, right))
min = (minimumCost(array, left - 1, right - 1) < minimumCost(array, left, right - 1)) ?
minimumCost(array, left - 1, right - 1) : minimumCost(array, left, right - 1);
else
min = (minimumCost(array, left - 1, right) < minimumCost(array, left, right - 1)) ?
minimumCost(array, left - 1, right) : minimumCost(array, left, right - 1);

return array[left][right] + min;
}
}

int main()
{
scanf("%d", &row);
scanf("%d", &col);
int array[row][col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
scanf("%d", &array[i][j]);
int left, right;
scanf("%d", &left);
scanf("%d", &right);

printf("%d", minimumCost(array, left, right));
return 0;
}

/*
Input:
row = 3
col = 3
array = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
left = 2
right = 2
Output:
15
Because to reach from (0, 0) to (2, 2)
the cost for minimum path is (0, 0) –> (1, 1) –> (2, 2); 1 + 5 + 9 = 15
*/
67 changes: 67 additions & 0 deletions Minimum_Cost_Path/Minimum_Cost_Path.cpp
@@ -0,0 +1,67 @@
/*
Finding minimum cost path in 2-D array "array[][]" to reach a position (left, right)
in array[][] from (0, 0).
Total cost of a path to reach (left, right) is sum of all the costs on that
path (including both source and destination).
*/

#include<iostream>
#include<limits.h>
using namespace std;

const int x = 10;

// Finding minimum cost path in 2-D array
int minimumCost(int array[][x], int left, int right)
{
int min;

// For invalid left and right query
if (left < 0 || right < 0)
return INT_MAX;
else if (left == 0 && right == 0)
return array[left][right];
else
{
// Finding path with minimum cost i.e. which way to move down, right and diagonally lower cells
if (minimumCost(array, left - 1, right - 1) < minimumCost(array, left - 1, right))
min = (minimumCost(array, left - 1, right - 1) < minimumCost(array, left, right - 1)) ?
minimumCost(array, left - 1, right - 1) : minimumCost(array, left, right - 1);
else
min = (minimumCost(array, left - 1, right) < minimumCost(array, left, right - 1)) ?
minimumCost(array, left - 1, right) : minimumCost(array, left, right - 1);

return array[left][right] + min;
}
}

int main()
{
int row, col, array[x][x];
cin>>row;
cin>>col;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
cin>>array[i][j];
int left, right;
cin>>left;
cin>>right;

cout<<minimumCost(array, left, right);
return 0;
}

/*
Input:
row = 3
col = 3
array = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
left = 2
right = 2
Output:
15
Because to reach from (0, 0) to (2, 2)
the cost for minimum path is (0, 0) –> (1, 1) –> (2, 2); 1 + 5 + 9 = 15
*/
63 changes: 63 additions & 0 deletions Minimum_Cost_Path/Minimum_Cost_Path.java
@@ -0,0 +1,63 @@
/*
Finding minimum cost path in 2-D array "array[][]" to reach a position (left, right)
in array[][] from (0, 0).
Total cost of a path to reach (left, right) is sum of all the costs on that
path (including both source and destination).
*/

import java.util.*;

class Min_Cost
{
// Finding minimum cost path in 2-D array
static int minimumCost(int array[][], int left, int right)
{
int min;
// For invalid left and right query
if (left < 0 || right < 0)
return Integer.MAX_VALUE;
else if (left == 0 && right == 0)
return array[left][right];
else
{
// Finding path with minimum cost i.e. which way to move down, right and diagonally lower cells
if (minimumCost(array, left - 1, right - 1) < minimumCost(array, left - 1, right))
min = (minimumCost(array, left - 1, right - 1) < minimumCost(array, left, right - 1)) ?
minimumCost(array, left - 1, right - 1) : minimumCost(array, left, right - 1);
else
min = (minimumCost(array, left - 1, right) < minimumCost(array, left, right - 1)) ?
minimumCost(array, left - 1, right) : minimumCost(array, left, right - 1);

return array[left][right] + min;
}
}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
int[][] array = new int[row][col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
array[i][j] = sc.nextInt();
int left = sc.nextInt();
int right = sc.nextInt();
System.out.print(minimumCost(array, left, right));
}
}

/*
Input:
row = 3
col = 3
array = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
left = 2
right = 2
Output:
15
Because to reach from (0, 0) to (2, 2)
the cost for minimum path is (0, 0) –> (1, 1) –> (2, 2); 1 + 5 + 9 = 15
*/
54 changes: 54 additions & 0 deletions Minimum_Cost_Path/Minimum_Cost_Path.py
@@ -0,0 +1,54 @@
'''
Finding minimum cost path in 2-D array "array[][]" to reach a position (left, right)
in array[][] from (0, 0).
Total cost of a path to reach (left, right) is sum of all the costs on that
path (including both source and destination).
'''

import sys

# Finding minimum cost path in 2-D array
def minimumCost(array, left, right):
# For invalid left and right query
if (left < 0 or right < 0):
return sys.maxsize
elif (left == 0 and right == 0):
return array[left][right]
else:
# Finding path with minimum cost i.e. which way to move down, right and diagonally lower cells
x = minimumCost(array, left - 1, right - 1)
y = minimumCost(array, left - 1, right)
z = minimumCost(array, left, right - 1)
if (x < y):
minimum = x if (x < z) else z
else:
minimum = y if (y < z) else z
return array[left][right] + minimum

# Driver program
row = int(input())
col = int(input())
array = []
for i in range(row):
a = []
for j in range(col):
a.append(int(input()))
array.append(a)
left = int(input())
right = int(input())
print(minimumCost(array, left, right))

'''
Input:
row = 3
col = 3
array = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
left = 2
right = 2
Output:
15
Because to reach from (0, 0) to (2, 2)
the cost for minimum path is (0, 0) –> (1, 1) –> (2, 2); 1 + 5 + 9 = 15
'''