• [LintCode] 477. Surrounded Regions


    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.

    Example

    Example 1:

    Input:
      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
    

    Example 2:

    Input:
      X X X X
      X O O X
      X O O X
      X O X X
    Output:
      X X X X
      X O O X
      X O O X
      X O X X


    public class Solution {
        /*
         * @param board: board a 2D board containing 'X' and 'O'
         * @return: nothing
         */
        int row;
        int col;
        public void surroundedRegions(char[][] board) {
            // write your code here
            if (board.length == 0 || board[0].length == 0) {
                return;
            }
            row = board.length;
            col = board[0].length;
            Queue<Cell> queue = new LinkedList<>();
            for (int i = 0; i < col; i++) {
                enQueue(0, i, queue, board);
                enQueue(row - 1, i, queue, board);
            }
            for (int i = 1; i < row - 1; i++) {
                enQueue(i, 0, queue, board);
                enQueue(i, col - 1, queue, board);
            }
            int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
            while (!queue.isEmpty()) {
                Cell cur = queue.poll();
                // if (grid[cur.x][cur.y] == 'O') {
                board[cur.x][cur.y] = 'T';
                // }
                for (int[] direction: directions) {
                    int nxtX = cur.x + direction[0];
                    int nxtY = cur.y + direction[1];
                    enQueue(nxtX, nxtY, queue, board);
                    // if (nxtX >= 0 && nxtX < row && nxtY >= 0 && nxtY < col && board[nxtX][nxtY] == 'O') {
                    //     board[nxtX][nxtY] = 'T';
                    // }
                }
            }
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (board[i][j] == 'T') {
                        board[i][j] = 'O';
                    } else if (board[i][j] == 'O') {
                        board[i][j] = 'X';
                    }
                }
            }
        }
        
        private void enQueue(int x, int y, Queue<Cell> queue, char[][] grid) {
            if (x >= 0 && x < row && y >= 0 && y < col && grid[x][y] == 'O') {
                queue.offer(new Cell(x, y));
            }
        }
    }
    
    class Cell {
        int x;
        int y;
        public Cell(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
  • 相关阅读:
    分布式算法(一致性Hash算法)
    浅析Postgres中的并发控制(Concurrency Control)与事务特性(上)
    PostgreSQL内核分析——BTree索引
    源码安装postgresql数据库
    QEMU漏洞挖掘
    mysql远程连接数据库
    C++学习之路(十一):C++的初始化列表
    搭建本地git服务器
    C++面试常见问题
    SkipList 跳表
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12564792.html
Copyright © 2020-2023  润新知