Surrounded Regions——LeetCode

Given a 2D 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.

For example,

X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X

题目大意:给一个棋盘,上面有'X'和'O',找出所有被'X'包围的'O',并替换成'X'。

解题思路:

解法一:从边开始,找出在四条边上的O,以BFS的方式找出所有与边上的O连通的O,这些O都是不被包围的,将这些O替换为某个特殊字符,遍历完之后,将O替换为X,将特殊字符替换为O。

解法二:直接用BFS遍历,找到一个O之后,加入BFS队列,找出与之连通的所有的O同时判断这些O是否被包围(即是否位于某条边上),将这些O加入一个List,每一个连通的O区域用一个surround布尔变量表示这个连通区域是否是被surround的,如果是就把这些O替换为X,否则不替换并继续遍历其他。

注意:这里因为要把i、j下标放入队列或者结果集,一开始我使用String类型key= i+"_"+j 来处理,后来看到别人key = i * colLen + j ; colLen是棋盘的列的数量,然后用key/colLen,key%colLen来得到i,j下标效率更高。

Talk is cheap>>

解法一:

  public void solve(char[][] board) {
        Queue<Integer> queue = new ArrayDeque<>();
        int rowLen = board.length;
        if (rowLen == 0)
            return;
        int colLen = board[0].length;
        int[][] adj = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
        for (int i = 0; i < rowLen; i++) {
            for (int j = 0; j < colLen; j++) {
          //如果O在四条边上
if (board[i][j] == 'O' && (i == 0 || j == 0 || i == rowLen - 1 || j == colLen - 1)) { int key = i * colLen + j; board[i][j] = '1'; queue.add(key); while (!queue.isEmpty()) {
              //BFS遍历与边上的O连通的O key
= queue.poll(); int x = key / colLen; int y = key % colLen; for (int k = 0; k < 4; k++) { int adj_x = x + adj[k][0]; int adj_y = y + adj[k][1]; if (adj_x >= 0 && adj_y >= 0 && adj_x < rowLen && adj_y < colLen) { if (board[adj_x][adj_y] == 'O') { int pos = adj_x * colLen + adj_y; board[adj_x][adj_y]='1'; queue.add(pos); } } } } } } } for (int i = 0; i < rowLen; i++) { for (int j = 0; j < colLen; j++) { if (board[i][j] == 'O') { board[i][j] = 'X'; } else if (board[i][j] == '1') { board[i][j] = 'O'; } } } }

解法二:

  public void solve(char[][] board) {
        Queue<Integer> queue = new ArrayDeque<>();
        int rowLen = board.length;
        if (rowLen == 0) {
            return;
        }
        int[][] adj = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
        int colLen = board[0].length;
        boolean[][] visited = new boolean[rowLen][colLen];
        for (int i = 0; i < rowLen; i++) {
            for (int j = 0; j < colLen; j++) {
                if (!visited[i][j] && board[i][j] == 'O') {
            //标准的BFS遍历
boolean surround = true; List<Integer> toHandle = new ArrayList<>(); queue.add(i * colLen + j); while (!queue.isEmpty()) { int key = queue.poll(); toHandle.add(key); int x = key / colLen; int y = key % colLen; for (int k = 0; k < 4; k++) {
                //检查O的上下左右
int adj_x = x + adj[k][0]; int adj_y = y + adj[k][1]; if (adj_x >= 0 && adj_y >= 0 && adj_x < rowLen && adj_y < colLen) {
                  //都不在边上
if (board[adj_x][adj_y] == 'O' && !visited[adj_x][adj_y]) { int pos = adj_x * colLen + adj_y; queue.add(pos); visited[adj_x][adj_y] = true; } } else {
                  //有一个在边上,置surround=false surround
= false; } } } if (surround) { for (int key : toHandle) { board[key / colLen][key % colLen] = 'X'; } } } } } }
原文地址:https://www.cnblogs.com/aboutblank/p/4394511.html