forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_130.java
157 lines (144 loc) · 6.26 KB
/
_130.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
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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class _130 {
public static class Solution1 {
/**
* I won't call this problem hard, it's just confusing, you'll definitely want to clarify what
* the problem means before coding. This problem actually means: any grid that is 'O' but on
* the four edges, will never be marked to 'X'; furthermore, any grid that is 'O' and that is
* connected with the above type of 'O' will never be marked to 'X' as well; only all other
* nodes that has any one direct neighbor that is an 'X' will be marked to 'X'.
*/
int[] dirs = new int[]{0, 1, 0, -1, 0};
public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) {
return;
}
int m = board.length;
int n = board[0].length;
Queue<int[]> queue = new LinkedList();
//check first row and last row and mark all those '0' on these two rows to be '+' to let them be different from other 'O',
//at the same time, we put them into the queue to get ready for a BFS to mark all those adjacent 'O' nodes to '+' as well
for (int j = 0; j < n; j++) {
if (board[0][j] == 'O') {
board[0][j] = '+';
queue.offer(new int[]{0, j});
}
if (board[m - 1][j] == 'O') {
board[m - 1][j] = '+';
queue.offer(new int[]{m - 1, j});
}
}
//check first column and last column too
for (int i = 0; i < m; i++) {
if (board[i][0] == 'O') {
board[i][0] = '+';
queue.offer(new int[]{i, 0});
}
if (board[i][n - 1] == 'O') {
board[i][n - 1] = '+';
queue.offer(new int[]{i, n - 1});
}
}
while (!queue.isEmpty()) {
int[] curr = queue.poll();
for (int i = 0; i < 4; i++) {
int x = curr[0] + dirs[i];
int y = curr[1] + dirs[i + 1];
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') {
board[x][y] = '+';
queue.offer(new int[]{x, y});
}
}
}
//now we can safely mark all other 'O' to 'X', also remember to put those '+' back to 'O'
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == '+') {
board[i][j] = 'O';
}
}
}
}
}
public static class Solution2 {
/**
* My completely original solution on 11/1/2021, again, using a pen and paper to visualize my thought process and list out all key steps helps a lot!
* 1. scan through this board;
* 2. whenever we find an 'O', we'll do BFS to find all connected points and use the first 'O' as its head point for this entire connected region;
* 3. whenever we visit a point, mark it as visited.
*/
public void solve(char[][] board) {
int m = board.length;
int n = board[0].length;
boolean[][] visited = new boolean[m][n];
Map<Integer, List<int[]>> headMap = new HashMap<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j] && board[i][j] == 'O') {
boolean capturable = bfs(i, j, board, visited, headMap);
if (capturable) {
capture(headMap, board);
}
}
}
}
}
private void capture(Map<Integer, List<int[]>> headMap, char[][] board) {
int m = board.length;
int n = board[0].length;
for (int head : headMap.keySet()) {
List<int[]> list = headMap.get(head);
for (int[] p : list) {
board[p[0]][p[1]] = 'X';
}
int x = head / m;
int y = head % n;
board[x][y] = 'X';
}
}
private boolean bfs(int startI, int startJ, char[][] board, boolean[][] visited, Map<Integer, List<int[]>> headMap) {
boolean capturable = true;
Queue<int[]> queue = new LinkedList<>();
int m = board.length;
int n = board[0].length;
queue.offer(new int[]{startI, startJ});
int head = startI * n + startJ;
List<int[]> list = headMap.getOrDefault(head, new ArrayList<>());
list.add(new int[]{startI, startJ});
int[] directions = new int[]{0, 1, 0, -1, 0};
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
int[] curr = queue.poll();
if (curr[0] == 0 || curr[0] == m - 1 || curr[1] == 0 || curr[1] == n - 1) {
capturable = false;
}
visited[curr[0]][curr[1]] = true;
for (int j = 0; j < directions.length - 1; j++) {
int newx = directions[j] + curr[0];
int newy = directions[j + 1] + curr[1];
if (newx >= 0 && newx < m && newy >= 0 && newy < n && !visited[newx][newy] && board[newx][newy] == 'O') {
queue.offer(new int[]{newx, newy});
visited[newx][newy] = true;
list.add(new int[]{newx, newy});
}
}
}
}
if (!capturable) {
headMap.remove(head);
} else {
headMap.put(head, list);
}
return capturable;
}
}
}