-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumFourWays.java
More file actions
71 lines (55 loc) · 2.21 KB
/
PathSumFourWays.java
File metadata and controls
71 lines (55 loc) · 2.21 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
package dev;
import java.util.*;
public class PathSumFourWays {
static class Cell implements Comparable<Cell> {
int row, col, cost;
Cell(int row, int col, int cost) {
this.row = row;
this.col = col;
this.cost = cost;
}
public int compareTo(Cell other) {
return Integer.compare(this.cost, other.cost);
}
}
public static int minimalPathSum(int[][] matrix) {
int n = matrix.length;
int[][] dist = new int[n][n];
for (int[] row : dist) Arrays.fill(row, Integer.MAX_VALUE);
PriorityQueue<Cell> pq = new PriorityQueue<>();
pq.offer(new Cell(0, 0, matrix[0][0]));
dist[0][0] = matrix[0][0];
int[] dRow = {-1, 1, 0, 0};
int[] dCol = {0, 0, -1, 1};
while (!pq.isEmpty()) {
Cell current = pq.poll();
for (int i = 0; i < 4; i++) {
int newRow = current.row + dRow[i];
int newCol = current.col + dCol[i];
if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < n) {
int newCost = dist[current.row][current.col] + matrix[newRow][newCol];
if (newCost < dist[newRow][newCol]) {
dist[newRow][newCol] = newCost;
pq.offer(new Cell(newRow, newCol, newCost));
}
}
}
}
return dist[n - 1][n - 1];
}
public static void main(String[] args) throws Exception {
// Load the matrix from a file or define it directly here
// Here's an example for loading a matrix from a file:
Scanner scanner = new Scanner(new java.io.File("C:\\Users\\ha\\Desktop\\0083_matrix.txt"));
List<int[]> matrixList = new ArrayList<>();
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(",");
int[] row = Arrays.stream(parts).mapToInt(Integer::parseInt).toArray();
matrixList.add(row);
}
scanner.close();
int[][] matrix = matrixList.toArray(new int[0][0]);
int result = minimalPathSum(matrix);
System.out.println("Minimal path sum: " + result);
}
}