-
Notifications
You must be signed in to change notification settings - Fork 87
/
LeeAlgorithmOptimized.java
67 lines (55 loc) · 2.34 KB
/
LeeAlgorithmOptimized.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
import java.util.LinkedList;
import java.util.Queue;
public class LeeAlgorithmOptimized {
private static final int[] dx = {-1, 0, 1, 0}; // Array to represent movements in the x-axis
private static final int[] dy = {0, 1, 0, -1}; // Array to represent movements in the y-axis
private static boolean isValid(int[][] matrix, int row, int col, boolean[][] visited) {
int numRows = matrix.length;
int numCols = matrix[0].length;
// Check if the position is within the matrix bounds and not visited before
return (row >= 0 && row < numRows && col >= 0 && col < numCols && matrix[row][col] == 0 && !visited[row][col]);
}
public static void leeAlgorithm(int[][] matrix, int startX, int startY) {
int numRows = matrix.length;
int numCols = matrix[0].length;
boolean[][] visited = new boolean[numRows][numCols];
Queue<Integer> queueX = new LinkedList<>();
Queue<Integer> queueY = new LinkedList<>();
queueX.add(startX); // Initialize the queues with the start position
queueY.add(startY);
visited[startX][startY] = true;
while (!queueX.isEmpty()) {
int x = queueX.poll(); // Get the current position
int y = queueY.poll();
for (int i = 0; i < 4; i++) {
int newX = x + dx[i]; // Calculate the new position
int newY = y + dy[i];
if (isValid(matrix, newX, newY, visited)) {
queueX.add(newX); // Add the new position to the queue
queueY.add(newY);
visited[newX][newY] = true;
matrix[newX][newY] = -1; // Mark the position as visited
}
}
}
}
public static void main(String[] args) {
int[][] matrix = {
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 1, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 0, 0}
};
int startX = 1;
int startY = 1;
leeAlgorithm(matrix, startX, startY);
// Print the modified matrix after running the algorithm
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}