File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public void solve (char [][] board ) {
3
+ if (board == null || board .length == 0 ) {
4
+ return ;
5
+ }
6
+
7
+ int m = board .length , n = board [0 ].length ;
8
+
9
+ for (int i = 0 ; i < m ; i ++) {
10
+ if (board [i ][0 ] == 'O' ) {
11
+ dfs (board , i , 0 );
12
+ }
13
+ if (board [i ][n - 1 ] == 'O' ) {
14
+ dfs (board , i , n - 1 );
15
+ }
16
+ }
17
+
18
+ for (int j = 1 ; j < n - 1 ; j ++) {
19
+ if (board [0 ][j ] == 'O' ) {
20
+ dfs (board , 0 , j );
21
+ }
22
+ if (board [m - 1 ][j ] == 'O' ) {
23
+ dfs (board , m - 1 , j );
24
+ }
25
+ }
26
+
27
+ for (int i = 0 ; i < m ; i ++) {
28
+ for (int j = 0 ; j < n ; j ++) {
29
+ if (board [i ][j ] == 'O' ) {
30
+ board [i ][j ] = 'X' ;
31
+ }
32
+ if (board [i ][j ] == '*' ) {
33
+ board [i ][j ] = 'O' ;
34
+ }
35
+ }
36
+ }
37
+ }
38
+
39
+ private void dfs (char [][] board , int x , int y ) {
40
+ if (x < 0 || y < 0 || x >= board .length || y >= board [x ].length || board [x ][y ] != 'O' ) {
41
+ return ;
42
+ }
43
+
44
+ board [x ][y ] = '*' ;
45
+
46
+ dfs (board , x - 1 , y );
47
+ dfs (board , x + 1 , y );
48
+ dfs (board , x , y - 1 );
49
+ dfs (board , x , y + 1 );
50
+ }
51
+ }
You can’t perform that action at this time.
0 commit comments