-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution130.cs
77 lines (66 loc) · 1.76 KB
/
Solution130.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
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
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution130
{
/// <summary>
/// 130. Surrounded Regions - Medium
/// <a href="https://leetcode.com/problems/surrounded-regions">See the problem</a>
/// </summary>
public void Solve(char[][] board)
{
if (board.Length == 0 || board[0].Length == 0)
{
return;
}
var rows = board.Length;
var cols = board[0].Length;
for (var i = 0; i < rows; i++)
{
if (board[i][0] == 'O')
{
DFS(board, i, 0);
}
if (board[i][cols - 1] == 'O')
{
DFS(board, i, cols - 1);
}
}
for (var j = 0; j < cols; j++)
{
if (board[0][j] == 'O')
{
DFS(board, 0, j);
}
if (board[rows - 1][j] == 'O')
{
DFS(board, rows - 1, j);
}
}
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < cols; j++)
{
if (board[i][j] == 'O')
{
board[i][j] = 'X';
}
else if (board[i][j] == '1')
{
board[i][j] = 'O';
}
}
}
}
private void DFS(char[][] board, int i, int j)
{
if (i < 0 || i >= board.Length || j < 0 || j >= board[0].Length || board[i][j] != 'O')
{
return;
}
board[i][j] = '1';
DFS(board, i - 1, j);
DFS(board, i + 1, j);
DFS(board, i, j - 1);
DFS(board, i, j + 1);
}
}