forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
41 lines (37 loc) · 1.05 KB
/
Solution.cs
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
public class Solution {
private readonly int[] dirs = {-1, 0, 1, 0, -1};
private char[][] board;
private int m;
private int n;
public void Solve(char[][] board) {
m = board.Length;
n = board[0].Length;
this.board = board;
for (int i = 0; i < m; ++i) {
Dfs(i, 0);
Dfs(i, n - 1);
}
for (int j = 0; j < n; ++j) {
Dfs(0, j);
Dfs(m - 1, j);
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == '.') {
board[i][j] = 'O';
} else if (board[i][j] == 'O') {
board[i][j] = 'X';
}
}
}
}
private void Dfs(int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] != 'O') {
return;
}
board[i][j] = '.';
for (int k = 0; k < 4; ++k) {
Dfs(i + dirs[k], j + dirs[k + 1]);
}
}
}