Title:
Given a 2D board containing ‘X‘
‘O‘
and, capture all regions surrounded by ‘X‘
.
A region was captured by flipping all ‘O‘
s into ‘X‘
s-surrounded region.
For example,
x x x xx o o xx x o xx o x x
After running your function, the board should is:
x x x xx x x xx x x xx O x x
Test Instructions:
Given a 2-dimensional planar inclusion ‘X‘
and ‘O‘,填充所有的被‘X‘
包围的区域
.
Like what
x x x xx o o xx x o xx o x x
After you run your function, the plane becomes:
x x x xx o o xx x o xx o x x
Algorithm Analysis:
* Typical BFS topics. Traversing each character, if "O", starts the BFS traversal from the current character and joins the currently traversed queue if it is also surrounded by "o".
* until all adjacent "O" are traversed, at the same time, to determine whether each o is surrounded, as long as by an O is not surrounded by,
* The currently traversed set of O is not surrounded because these o are connected.
AC Code:
<span style= "Font-family:microsoft yahei;font-size:12px;" >public class Solution {public void Solve (char[][] board) {if (board==null| | board.length==0) return; int row=board.length; int col=board[0].length; Boolean visited[][] = new Boolean[row][col];//Traversal tag array for (int i=0;i<row;i++) {for (int J=0;j<co l;j++) {if (board[i][j]== ' O ' && (!visited[i][j])) {BFS (Board,i,j,vi sited);//If not traversed, then breadth-first search for the node}}}}private void BFs (char[][] board,int i,int J,boolean visited[] []) {arraylist<integer> list =new arraylist<integer> (); queue<integer> queue = new Linkedlist<integer> (); Boolean label=true; int row=board.length; int Col=board[0].length;int TEMJUDGE,TEMI,TEMJ; Queue.add (I*COL+J);//Convert the two-dimensional array position to one-dimensional visited[i][j]=true;while (!queue.isempty ()) {temjudge=queue.poll (); List.add ( Temjudge); temj=temjudge%col;//column Position TeMi= (TEMJUDGE-TEMJ)/col;//line position if (temi==0| | temj==0| | temi==row-1| | TEMJ==COL-1) label=false;//If the node is at the boundary, this is not surrounded, so all the nodes that are traversed do not change, here is a label to record if (temj>0&&board[temi][temj-1 ]== ' O ' &&!visited[temi][temj-1])//left {Queue.add (temi*col+temj-1); visited[temi][temj-1]=true;} if (temi>0&&board[temi-1][temj]== ' O ' &&!visited[temi-1][temj])//on {Queue.add (temi-1) *COL+TEMJ ); visited[temi-1][temj]=true;} if (temj<board[0].length-1&&board[temi][temj+1]== ' O ' &&!visited[temi][temj+1])//Right {Queue.add ( temi*col+temj+1); visited[temi][temj+1]=true;} if (temi<board.length-1&&board[temi+1][temj]== ' O ' &&!visited[temi+1][temj])//under {Queue.add ( temi+1) *col+temj); visited[temi+1][temj]=true;}} If the (label)//traversal of all the nodes is not at the boundary, this is a change to these nodes {for (int k=0;k<list.size (); k++) {temjudge=list.get (k); temj=temjudge% Col Temi= (TEMJUDGE-TEMJ)/col;board[temi][temj]= ' X ';}} }}</span>
Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source
[Leetcode] [Java] Surrounded regions