130. Surrounded Regions
Posted tobeabetterpig
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了130. Surrounded Regions相关的知识,希望对你有一定的参考价值。
Given a 2D board containing ‘X‘ and ‘O‘ (the letter O), capture all regions surrounded by ‘X‘. A region is captured by flipping all ‘O‘s into ‘X‘s in that surrounded region. 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 Explanation: Surrounded regions shouldn’t 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. class Solution { private static final int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; public void solve(char[][] board) { // sanity check if(board == null || board.length == 0 || board[0].length == 0) return; // the Os on the boarder will still stay O . do bfs and store them in a quueue, store their coordinates // base case, when neither dir is O anymore // the Os which are not in the queue can be converted to X boolean[][] visited = new boolean[board.length][board[0].length]; Queue<int[]> queue = new LinkedList<>(); // traverse the 4 borders and add the O‘s coordinates on the boarder into the queue // up : row : 0, col from 0 to board[0].length for(int j = 0; j < board[0].length; j++){ if(board[0][j] == ‘O‘){ queue.offer(new int[]{0, j}); visited[0][j] = true; } } // down : row: board.length -1, col from 0, to board[0].length for(int j = 0; j < board[0].length; j++){ if(board[board.length - 1][j] == ‘O‘){ queue.offer(new int[]{board.length -1, j}); visited[board.length - 1][j] = true; } } // left : row from 0 to board.length , col is 0 for(int i = 0; i < board.length; i++){ if(board[i][0] == ‘O‘){ queue.offer(new int[]{i, 0}); visited[i][0] = true; } } // right : row from 0 to board.length , col is board[0].length for(int i = 0; i < board.length; i++){ if(board[i][board[0].length - 1] == ‘O‘) { queue.offer(new int[]{i, board[0].length - 1}); visited[i][board[0].length - 1] = true; } } // bfs from boarder, convert O to E , bfs(queue, board, visited); // after bfs, convert O to X, and convert E to O convert(board); } private void bfs(Queue<int[]> queue, char[][] board, boolean[][] visited){ while(!queue.isEmpty()){ int[] cur = queue.poll(); int row = cur[0]; int col = cur[1]; board[row][col] = ‘E‘; for(int[] dir : dirs){ int newRow = row + dir[0]; int newCol = col + dir[1]; // check boundary and if its O, and if visited alredy if(newRow < 0 || newCol < 0 || newRow >= board.length || newCol >= board[0].length || board[newRow][newCol] != ‘O‘ ||visited[newRow][newCol]) continue; queue.offer(new int[]{newRow, newCol}); } } } private void convert(char[][] board){ for(int i = 0; i < board.length; i++){ for(int j = 0; j < board[0].length; j++){ if(board[i][j] == ‘O‘) board[i][j] = ‘X‘; if(board[i][j] == ‘E‘) board[i][j] = ‘O‘; } } } }
以上是关于130. Surrounded Regions的主要内容,如果未能解决你的问题,请参考以下文章