-
Notifications
You must be signed in to change notification settings - Fork 16
/
SurroundedRegions.java
92 lines (78 loc) · 3.12 KB
/
SurroundedRegions.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
// https://leetcode.com/problems/surrounded-regions
// T: O(m * n)
// S: O(m * n)
import java.util.HashSet;
import java.util.Set;
public class SurroundedRegions {
private record Position(int row, int column) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
Position position = (Position) o;
if (row != position.row) return false;
return column == position.column;
}
@Override
public int hashCode() {
return 31 * row + column;
}
public Position top() {
return new Position(row - 1, column);
}
public Position right() {
return new Position(row, column + 1);
}
public Position bottom() {
return new Position(row + 1, column);
}
public Position left() {
return new Position(row, column - 1);
}
}
public void solve(char[][] board) {
final Set<Position> visited = new HashSet<>();
final Set<Position> current = new HashSet<>();
final int rows = board.length, columns = board[0].length;
for (int row = 1 ; row < rows - 1 ; row++) {
for (int column = 1 ; column < columns - 1 ; column++) {
if (board[row][column] == 'O') {
final boolean isBorderRegion = markRegion(board, new Position(row, column), visited, current);
if (!isBorderRegion) {
captureRegion(board, current);
}
current.clear();
}
}
}
}
private boolean markRegion(char[][] board, Position position, Set<Position> visited, Set<Position> region) {
if (isInvalidPosition(board, position) || visited.contains(position) || isX(board, position)) return false;
visited.add(position);
region.add(position);
boolean top = markRegion(board, position.top(), visited, region);
boolean right = markRegion(board, position.right(), visited, region);
boolean bottom = markRegion(board, position.bottom(), visited, region);
boolean left = markRegion(board, position.left(), visited, region);
return top || right || bottom || left || isBorderPosition(board, position);
}
private boolean isX(char[][] board, Position position) {
return board[position.row][position.column] == 'X';
}
private boolean isBorderPosition(char[][] board, Position position) {
return position.row == 0
|| position.row == board.length - 1
|| position.column == 0
|| position.column == board[0].length - 1;
}
private boolean isInvalidPosition(char[][] board, Position position) {
return position.row < 0
|| position.row >= board.length
|| position.column < 0
|| position.column >= board[0].length;
}
private void captureRegion(char[][] board, Set<Position> positions) {
for (Position position : positions) {
board[position.row][position.column] = 'X';
}
}
}