2021-10-14:被围绕的区域。给你一个 m x n 的矩阵 board ,由若干字符 'X' 和 'O' ,找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。力扣130。

福大大 答案2021-10-14:

从四周边界开始感染,没感染到的区域,变成'X'。 时间复杂度:O(MN)。 空间复杂度:未知。

代码用golang编写。代码如下:

package main

import "fmt"

func main() {
    if true {
        board := [][]byte{{'X', 'X', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'X', 'O', 'X'}, {'X', 'O', 'X', 'X'}}
        solve1(board)
        for i := 0; i < len(board); i++ {
            for j := 0; j < len(board[i]); j++ {
                fmt.Printf("%c ", board[i][j])
            }
            fmt.Println("")
        }
    }
    fmt.Println("---------")
    if true {
        board := [][]byte{{'X', 'X', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'X', 'O', 'X'}, {'X', 'O', 'X', 'X'}}
        solve2(board)
        for i := 0; i < len(board); i++ {
            for j := 0; j < len(board[i]); j++ {
                fmt.Printf("%c ", board[i][j])
            }
            fmt.Println("")
        }
    }

}

//  // m -> 二维数组, 不是0就是1
//  //
//  public static void infect(int[][] m, int i, int j) {
//      if (i < 0 || i == m.length || j < 0 || j == m[0].length || m[i][j] != 1) {
//          return;
//      }
//      // m[i][j] == 1
//      m[i][j] = 2;
//      infect(m, i - 1, j);
//      infect(m, i + 1, j);
//      infect(m, i, j - 1);
//      infect(m, i, j + 1);
//  }

func solve1(board [][]byte) {
    ans := make([]bool, 1)
    for i := 0; i < len(board); i++ {
        for j := 0; j < len(board[0]); j++ {
            if board[i][j] == 'O' {
                ans[0] = true
                can(board, i, j, ans)
                if ans[0] {
                    board[i][j] = 'T'
                } else {
                    board[i][j] = 'F'
                }
            }
        }
    }
    for i := 0; i < len(board); i++ {
        for j := 0; j < len(board[0]); j++ {
            can := board[i][j]
            if can == 'T' || can == 'F' {
                board[i][j] = '.'
                change(board, i, j, can)
            }
        }
    }

}

func can(board [][]byte, i int, j int, ans []bool) {
    if i < 0 || i == len(board) || j < 0 || j == len(board[0]) {
        ans[0] = false
        return
    }
    if board[i][j] == 'O' {
        board[i][j] = '.'
        can(board, i-1, j, ans)
        can(board, i+1, j, ans)
        can(board, i, j-1, ans)
        can(board, i, j+1, ans)
    }
}

func change(board [][]byte, i int, j int, can byte) {
    if i < 0 || i == len(board) || j < 0 || j == len(board[0]) {
        return
    }
    if board[i][j] == '.' {
        if can == 'T' {
            board[i][j] = 'X'
        } else {
            board[i][j] = '0'
        }
        change(board, i-1, j, can)
        change(board, i+1, j, can)
        change(board, i, j-1, can)
        change(board, i, j+1, can)
    }
}

// 从边界开始感染的方法,比第一种方法更好
func solve2(board [][]byte) {
    if len(board) == 0 || len(board[0]) == 0 {
        return
    }
    N := len(board)
    M := len(board[0])
    for j := 0; j < M; j++ {
        if board[0][j] == 'O' {
            free(board, 0, j)
        }
        if board[N-1][j] == 'O' {
            free(board, N-1, j)
        }
    }
    for i := 1; i < N-1; i++ {
        if board[i][0] == 'O' {
            free(board, i, 0)
        }
        if board[i][M-1] == 'O' {
            free(board, i, M-1)
        }
    }
    for i := 0; i < N; i++ {
        for j := 0; j < M; j++ {
            if board[i][j] == 'O' {
                board[i][j] = 'X'
            }
            if board[i][j] == 'F' {
                board[i][j] = 'O'
            }
        }
    }
}

func free(board [][]byte, i int, j int) {
    if i < 0 || i == len(board) || j < 0 || j == len(board[0]) || board[i][j] != 'O' {
        return
    }
    board[i][j] = 'F'
    free(board, i+1, j)
    free(board, i-1, j)
    free(board, i, j+1)
    free(board, i, j-1)
}

执行结果如下: 图片


左神java代码