-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmillionairemadness.java
More file actions
157 lines (125 loc) · 5.14 KB
/
Copy pathmillionairemadness.java
File metadata and controls
157 lines (125 loc) · 5.14 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedOutputStream;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Arrays;
import java.util.Queue;
import java.util.LinkedList;
public class millionairemadness {
public static final int[][] DIRS = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedOutputStream out = new BufferedOutputStream(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
int m = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
int[][] weightGrid = new int[m][n];
int[][] vertexGrid = new int[m][n];
int vertex = 0;
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
vertexGrid[i][j] = vertex++;
weightGrid[i][j] = Integer.parseInt(st.nextToken());
}
}
// make adjList for every coordinate in the grid
ArrayList<ArrayList<IntegerPair>> adjList = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
ArrayList<IntegerPair> tempList = new ArrayList<>();
for (int[] dir : DIRS) {
int nextRow = i + dir[0];
int nextCol = j + dir[1];
boolean isValidRow = nextRow >= 0 && nextRow < m;
boolean isValidCol = nextCol >= 0 && nextCol < n;
if (!isValidRow || !isValidCol) continue;
int weight = Math.max(0, weightGrid[nextRow][nextCol] - weightGrid[i][j]);
tempList.add(new IntegerPair(vertexGrid[nextRow][nextCol], weight));
}
adjList.add(tempList);
}
}
int ladderHeight = -1;
boolean[] visited = new boolean[m * n];
PriorityQueue<IntegerPair> pq = new PriorityQueue<>();
int source = vertexGrid[0][0], dest = vertexGrid[m - 1][n - 1];
// init
pq.add(new IntegerPair(source, 0));
while (!pq.isEmpty()) {
IntegerPair v = pq.poll();
ladderHeight = v.second();
// System.out.println(v.first());
// System.out.println(ladderHeight);
// DFS results in Memory Limit Exceeded (MLE)
BFS(v.first(), dest, visited, adjList, ladderHeight, pq);
// System.out.println(Arrays.toString(visited).replace("[", "").replace("]", "").replace("false", ".").replace("true", "T").replace(",", " "));
// System.out.println();
if (visited[dest]) break;
}
out.write(Integer.toString(ladderHeight).getBytes());
br.close();
out.close();
}
public static void DFS(int source, int dest, boolean[] visited,
ArrayList<ArrayList<IntegerPair>> adjList,
int heightToExplore,
PriorityQueue<IntegerPair> vertexToExplore) {
visited[source] = true;
if (source == dest) return;
for (IntegerPair v : adjList.get(source)) {
int val = v.first();
boolean isValid = heightToExplore >= v.second();
if (!isValid && !visited[val]) {
vertexToExplore.add(new IntegerPair(source, v.second()));
continue;
}
if (!visited[val]) {
DFS(val, dest, visited, adjList, heightToExplore, vertexToExplore);
}
}
}
public static void BFS(int source, int dest, boolean[] visited,
ArrayList<ArrayList<IntegerPair>> adjList,
int heightToExplore,
PriorityQueue<IntegerPair> vertexToExplore) {
Queue<Integer> q = new LinkedList<>();
q.add(source);
visited[source] = true;
while (!q.isEmpty()) {
int u = q.poll();
if (u == dest) return;
for (IntegerPair v : adjList.get(u)) {
int val = v.first();
boolean isValid = heightToExplore >= v.second();
if (!isValid && !visited[val]) {
vertexToExplore.add(new IntegerPair(u, v.second()));
continue;
}
if (!visited[val]) {
visited[val] = true;
q.add(val);
}
}
}
}
}
class IntegerPair implements Comparable<IntegerPair> {
protected Integer first;
protected Integer second;
public IntegerPair(Integer first, Integer second) {
this.first = first;
this.second = second;
}
// sort by weight
public int compareTo(IntegerPair o) {
return this.second() - o.second();
}
public Integer first() { return first; }
public Integer second() { return second; }
public String toString() {
return String.format("(%d %d)", first, second);
}
}