Given an m x n
matrix board
containing 'X'
and 'O'
, capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
Example 1:
Input: board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]] Output: [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]] Explanation: Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
Example 2:
Input: board = [["X"]] Output: [["X"]]
Constraints:
m == board.length
n == board[i].length
1 <= m, n <= 200
board[i][j]
is'X'
or'O'
.
Union find.
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(board), len(board[0])
p = list(range(m * n + 1))
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
for i in range(m):
for j in range(n):
if board[i][j] == 'O':
if i == 0 or j == 0 or i == m - 1 or j == n - 1:
p[find(i * n + j)] = find(m * n)
else:
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
if board[i + x][j + y] == "O":
p[find(i * n + j)] = find((i + x) * n + j + y)
for i in range(m):
for j in range(n):
if board[i][j] == 'O' and find(i * n + j) != find(m * n):
board[i][j] = 'X'
Union find.
class Solution {
private int[] p;
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
public void solve(char[][] board) {
int m = board.length, n = board[0].length;
p = new int[m * n + 1];
for (int i = 0; i < p.length; ++i) {
p[i] = i;
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'O') {
if (i == 0 || j == 0 || i == m - 1 || j == n - 1) {
p[find(i * n + j)] = find(m * n);
} else {
for (int[] e : dirs) {
if (board[i + e[0]][j + e[1]] == 'O') {
p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
}
}
}
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'O' && find(i * n + j) != find(m * n)) {
board[i][j] = 'X';
}
}
}
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
/**
Do not return anything, modify board in-place instead.
*/
function solve(board: string[][]): void {
let m = board.length,
n = board[0].length;
if (m < 3 || n < 3) return;
let visited = Array.from({ length: m }, v => new Array(n).fill(false));
// 第一行,最后一行, 第一列, 最后一列
for (let i of [0, m - 1]) {
for (let j = 0; j < n; ++j) {
if (board[i][j] == "X") {
visited[i][j] = true;
} else {
dfs(board, i, j, visited, true);
}
}
}
for (let i = 0; i < m; ++i) {
for (let j of [0, n - 1]) {
if (board[i][j] == "X") {
visited[i][j] = true;
} else {
dfs(board, i, j, visited, true);
}
}
}
for (let i = 1; i < m - 1; ++i) {
for (let j = 1; j < n - 1; ++j) {
!visited[i][j] && dfs(board, i, j, visited);
}
}
}
function dfs(
board: string[][],
i: number,
j: number,
visited: boolean[][],
edge = false
): void {
let m = board.length,
n = board[0].length;
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || visited[i][j]) {
return;
}
visited[i][j] = true;
if (board[i][j] == "X") {
return;
}
if (!edge) {
board[i][j] = "X";
}
for (let [dx, dy] of [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
]) {
let x = i + dx,
y = j + dy;
dfs(board, x, y, visited, edge);
}
}
Union find.
class Solution {
public:
vector<int> p;
int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
void solve(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size();
p.resize(m * n + 1);
for (int i = 0; i < p.size(); ++i) p[i] = i;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (board[i][j] == 'O')
{
if (i == 0 || j == 0 || i == m - 1 || j == n - 1) p[find(i * n + j)] = find(m * n);
else
{
for (auto e : dirs)
{
if (board[i + e[0]][j + e[1]] == 'O') p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
}
}
}
}
}
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (board[i][j] == 'O' && find(i * n + j) != find(m * n)) board[i][j] = 'X';
}
}
}
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
};
Union find.
var p []int
func solve(board [][]byte) {
m, n := len(board), len(board[0])
p = make([]int, m*n+1)
for i := 0; i < len(p); i++ {
p[i] = i
}
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if board[i][j] == 'O' {
if i == 0 || j == 0 || i == m-1 || j == n-1 {
p[find(i*n+j)] = find(m * n)
} else {
for _, e := range dirs {
if board[i+e[0]][j+e[1]] == 'O' {
p[find(i*n+j)] = find((i+e[0])*n + j + e[1])
}
}
}
}
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if board[i][j] == 'O' && find(i*n+j) != find(m*n) {
board[i][j] = 'X'
}
}
}
}
func find(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}