-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinCostPathInGraph.java
141 lines (104 loc) · 3.48 KB
/
MinCostPathInGraph.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Problem Description
You are given a AB character matrix named C. Every cell in C has a character U,R,L or D indicating up,right,left and down.
Your target is to go from top left corner to the bottom right corner of the matrix.
But there are some restrictions while moving along the matrix:
If you follow what is written in the cell then you can move freely.
But if you don't follow what is written in the cell then you have to pay 1 unit of cost.
Like: If a cell contains character U and you go right instead of Up you have to pay 1 unit of cost.
So your task is to find the minimum cost to go from top-left corner to the bottom-right corner.
Problem Constraints
1 <= A,B <= 103
C[i][j] can be either U,R,L or D.
Input Format
First Argument represents a integer A (number of rows).
Second Argument represents a integer B (number of columns).
Third Argument represents a string array C which contains A strings each of length B.
Output Format
Return an integer denoting the minimum cost to travel from top-left corner to bottom-right corner.
Example Input
Input:1
A = 3
B = 3
C = ["RRR","DDD","UUU"]
Input:2
A = 1
B = 4
C = ["LLLL"]
Example Output
Output-1 :
1
Output-2 :
3
Example Explanation*
Explanation for Input-1:
Matrix looks like: RRR
DDD
UUU
We go right two times and down two times.
So from top-right cell we are going down though right is given so this incurs a cost of 1.
Explanation for Input-2:
Matrix looks like: LLLL
We go right three times.
*/
class Tri implements Comparable<Tri>{
int x;
int y;
int cost;
Tri(int x,int y,int cost){
this.x = x;
this.y = y;
this.cost = cost;
}
public int compareTo(Tri t){
return this.cost - t.cost;
}
}
public class Solution {
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
char dir[]={'U','R','D','L'};
public int solve(int A, int B, ArrayList<String> C) {
boolean[][] visited = new boolean[A][B];
int[][] cost = new int[A][B];
for(int[] c : cost){
Arrays.fill(c,Integer.MAX_VALUE);
}
PriorityQueue<Tri> pq = new PriorityQueue<>();
pq.add(new Tri(0,0,0));
visited[0][0] = true;
cost[0][0] = 0;
while(pq.size() > 0){
Tri t = pq.poll();
visited[t.x][t.y] = true;
if(t.x == A-1 && t.y == B-1){
return cost[t.x][t.y];
}
for(int i=0; i<4; i++){
int cx = t.x + dx[i];
int cy = t.y + dy[i];
if(isValid(cx,cy,A,B) && !visited[cx][cy]){
if(C.get(t.x).charAt(t.y) == dir[i]){
if(cost[t.x][t.y] < cost[cx][cy]){
pq.add(new Tri(cx,cy,cost[t.x][t.y]));
cost[cx][cy] = cost[t.x][t.y];
}
}
else{
if(cost[t.x][t.y]+1 < cost[cx][cy]){
pq.add(new Tri(cx,cy,cost[t.x][t.y] + 1));
cost[cx][cy] = 1 + cost[t.x][t.y];
}
}
}
}
}
return cost[A-1][B-1];
}
private boolean isValid(int x,int y,int A,int B){
if(x>=0 && x<A && y>=0 && y<B){
return true;
}
return false;
}
}