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
Title: Give a chessboard with ' X ' and ' O ', find all ' o ' surrounded by ' x ' and replace it with ' X '.
Problem Solving Ideas:
Solution one: From the edge, find out on the four side o, in the way of BFS to find all o connected to the Edge O, these o are not surrounded, the O is replaced with a special character, after traversing, the O is replaced by x, the special character is replaced with O.
Solution two: Directly with the BFS traversal, find an O, join the BFS queue, find out all the O connected with it at the same time to determine whether the O is surrounded (that is, whether on a certain edge), and add these o to a list, Each connected O-Zone uses a surround Boolean variable to indicate whether the connected area is surround, and if it is replaced with X, it is not replaced and continues to traverse the other.
Note: Here because I, j subscript into the queue or result set, I started with the string type key= i+ "_" +j to deal with, and later saw someone else key = i * Collen + j; Collen is the number of columns of the chessboard, and then uses Key/collen,key%collen to get the i,j subscript more efficient.
Talk is cheap>>
Solution One:
Public voidSolveChar[] board) {Queue<Integer> queue =NewArraydeque<>(); intRowlen =board.length; if(Rowlen = = 0) return; intCollen = board[0].length; int[] adj = {{0,-1}, {0, 1}, {-1, 0}, {1, 0}}; for(inti = 0; i < Rowlen; i++) { for(intj = 0; J < Collen; J + +) {
If O is on four sidesif(Board[i][j] = = ' O ' && (i = = 0 | | j = 0 | | i = = RowLen-1 | | j = = ColLen-1)) { intKey = i * Collen +J; BOARD[I][J]= ' 1 '; Queue.add (key); while(!Queue.isempty ()) {
BFS traversal O-Key with O-connected on edge=Queue.poll (); intx = key/Collen; inty = key%Collen; for(intk = 0; K < 4; k++) { intadj_x = x + adj[k][0]; intadj_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 ') { intpos = adj_x * Collen +adj_y; Board[adj_x][adj_y]= ' 1 '; Queue.add (POS); } } } } } } } for(inti = 0; i < Rowlen; i++) { for(intj = 0; J < Collen; J + +) { if(Board[i][j] = = ' O ') {Board[i][j]= ' X '; } Else if(Board[i][j] = = ' 1 ') {Board[i][j]= ' O '; } } } }
Solution Two:
Public voidSolveChar[] board) {Queue<Integer> queue =NewArraydeque<>(); intRowlen =board.length; if(Rowlen = = 0) { return; } int[] adj = {{0,-1}, {0, 1}, {-1, 0}, {1, 0}}; intCollen = board[0].length; Boolean[] visited =New Boolean[Rowlen][collen]; for(inti = 0; i < Rowlen; i++) { for(intj = 0; J < Collen; J + +) { if(!visited[i][j] && board[i][j] = = ' O ') {
Standard BFS TraversalBooleanSurround =true; List<Integer> Tohandle =NewArraylist<>(); Queue.add (i* Collen +j); while(!Queue.isempty ()) { intKey =Queue.poll (); Tohandle.add (key); intx = key/Collen; inty = key%Collen; for(intk = 0; K < 4; k++) {
Check o up or downintadj_x = x + adj[k][0]; intadj_y = y + adj[k][1]; if(adj_x >= 0 && adj_y >= 0 && adj_x < rowlen && Adj_y <Collen) {
is not on the edge.if(board[adj_x][adj_y] = = ' O ' &&!Visited[adj_x][adj_y]) { intpos = adj_x * Collen +adj_y; Queue.add (POS); Visited[adj_x][adj_y]=true; } } Else {
There is a surround=false surround on the edge .=false; } } } if(surround) { for(intkey:tohandle) {Board[key/collen][key% Collen] = ' X '; } } } } } }
Surrounded Regions--leetcode