-
Notifications
You must be signed in to change notification settings - Fork 0
/
MillionaireMadness.java
105 lines (94 loc) · 2.91 KB
/
MillionaireMadness.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
import java.util.PriorityQueue;
/**
* MillionaireMadness
*/
public class MillionaireMadness {
static int[][] dist;
public static void main(String[] args) {
Kattio sc = new Kattio(System.in, System.out);
int rows = sc.getInt();
int columns = sc.getInt();
dist = new int[rows][columns];
int[][] matrix = new int[rows][columns];
initSSP(rows, columns);
PriorityQueue<Vertex> pq = new PriorityQueue<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = sc.getInt();
}
}
pq.add(new Vertex(0, 0, 0));
dist[0][0] = 0;
int[] xdir = {1, 0, -1 ,0};
int[] ydir = {0, 1, 0, -1};
while (!pq.isEmpty()) {
Vertex[] adjVertex = new Vertex[4];
int j = 0;
Vertex vtx = pq.poll();
for (int i = 0; i < 4; i++) {
int xCord = vtx.x + xdir[i];
int yCord = vtx.y + ydir[i];
if (xCord >= 0 && xCord < rows) {
if (yCord >= 0 && yCord < columns) {
int val;
int nbVal = matrix[xCord][yCord];
int vtxVal = matrix[vtx.x][vtx.y];
if (nbVal > vtxVal + vtx.weight) {
val = matrix[xCord][yCord] - matrix[vtx.x][vtx.y];
} else {
val = vtx.weight;
}
adjVertex[j] = new Vertex(xCord, yCord, val);
j++;
}
}
}
for (Vertex item : adjVertex) {
if (item != null) {
Vertex res = relax(item, pq);
if (res != null) {
pq.offer(res);
}
}
}
}
sc.println(dist[rows - 1][columns - 1]);
sc.close();
}
public static void initSSP(int maxX, int maxY) {
for (int i = 0; i < maxX; i++) {
for (int j = 0; j < maxY; j++) {
dist[i][j] = Integer.MAX_VALUE;
}
}
}
public static Vertex relax(Vertex v, PriorityQueue<Vertex> pq) {
if (dist[v.x][v.y] > v.weight) {
pq.remove(new Vertex(v.x, v.y, dist[v.x][v.y]));
dist[v.x][v.y] = v.weight;
return v;
} else {
return null;
}
}
}
class Vertex implements Comparable<Vertex> {
int x;
int y;
int weight;
public Vertex(int a, int b, int w) {
this.x = a;
this.y = b;
this.weight = w;
}
@Override
public int compareTo(Vertex o) {
if (this.weight < o.weight) {
return -1;
} else if (this.weight == o.weight) {
return 0;
} else {
return 1;
}
}
}