-
Notifications
You must be signed in to change notification settings - Fork 5
/
MinimumCostPath.java
55 lines (54 loc) · 1.42 KB
/
MinimumCostPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*https://practice.geeksforgeeks.org/problems/minimum-cost-path3833/1*/
class Data implements Comparable<Data>
{
int i;
int j;
int cost;
Data(int i, int j, int cost)
{
this.i = i;
this.j = j;
this.cost = cost;
}
public int compareTo(Data d)
{
return this.cost-d.cost;
}
}
class Solution
{
//Function to return the minimum cost to react at bottom
//right cell from top left cell.
public int minimumCostPath(int[][] grid)
{
// Code here
//dijkstra algo
Data first = new Data(0,0,grid[0][0]);
PriorityQueue<Data> pq = new PriorityQueue<>();
pq.add(first);
int n = grid.length, i, j, k, x, y, cost;
int arr[][] = {{1,0},{-1,0},{0,1},{0,-1}};
boolean visited[][] = new boolean[n][n];
while (!pq.isEmpty())
{
Data pop = pq.poll();
i = pop.i;
j = pop.j;
cost = pop.cost;
if (visited[i][j]) continue;
if (i == n-1 && j == n-1) return cost;
visited[i][j] = true;
for (k = 0; k < 4; ++k)
{
x = i+arr[k][0];
y = j+arr[k][1];
if (x >= 0 && x < n && y >= 0 && y < n)
{
Data d = new Data(x,y,cost+grid[x][y]);
pq.add(d);
}
}
}
return -1;
}
}