# Leetcode:surrounded Regions Problem Solving Report

Source: Internet
Author: User

Surrounded regions

Given a 2D board containing ' x ' and ' O ', capture all regions surrounded by ' x '.

A region was captured by flipping all ' O ' s into the ' X ' s in this 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 is:

x x x x
x x x x
x x x x
x O x X

Solution 1:

Classic BFS Topics. If DFS is used, it times out.

Use the queue for BFS. Note that when judging the index out of bounds, the home page has written 2 ways.

(1) can be directly to index between 0-x*y. Here is a small trick, at the left edge of the point, index-1 will cause us to return to the far right of the previous line. Of course that's the point.

Edge points, so there is no solution error.

(2) Determine whether the point is not at the edge, determine whether there are no nodes up or down.

Frequently-faulted points: Calculate index is I * cols + j This is a good thing to remember.

`1  Public classSolution {2      Public voidSolveChar[] board) {3         if(board = =NULL|| Board.length = = 0 | | Board.length = = 0) {4             return;5         }6         7         introws =board.length;8         intcols = board.length;9         Ten         //The first line and the last line . One          for(intj = 0; J < cols; J + +) { ABFS (board, 0, j); -BFS (board, Rows-1, j); -         } the          -         //The left and right column -          for(inti = 0; i < rows; i++) { -BFS (board, I, 0); +BFS (Board, I, cols-1); -         } +          A         //capture all the nodes. at          for(inti = 0; i < rows; i++) { -              for(intj = 0; J < cols; J + +) { -                 if(Board[i][j] = = ' O ') { -BOARD[I][J] = ' X '; -}Else if(Board[i][j] = = ' B ') { -BOARD[I][J] = ' O '; in                 } -             } to         } +          -         return; the     } *      \$      Public voidBFS1 (Char[] board,intIintj) {Panax Notoginseng         introws =board.length; -         intcols = board.length; the          +queue<integer> q =NewLinkedlist<integer>(); AQ.offer (i * cols +j); the          +          while(!Q.isempty ()) { -             intindex =Q.poll (); \$              \$             //Index is out of bound. -             if(Index < 0 | | Index >= ROWS *cols) { -                 Continue; the             } -             Wuyi             intx = index/cols; the             inty = index%cols; -              Wu             if(Board[x][y]! = ' O ') { -                 Continue; About             } \$              -Board[x][y] = ' B '; -Q.offer (Index + 1); -Q.offer (index-1); AQ.offer (Index +cols); +Q.offer (Index-cols); the         } -     } \$      the      Public voidBFsChar[] board,intIintj) { the         introws =board.length; the         intcols = board.length; the          -queue<integer> q =NewLinkedlist<integer>(); inQ.offer (i * cols +j); the          the          while(!Q.isempty ()) { About             intindex =Q.poll (); the              the             intx = index/cols; the             inty = index%cols; +              -             if(Board[x][y]! = ' O ') { the                 Continue;Bayi             } the              theBoard[x][y] = ' B '; -             if(Y < Cols-1) { -Q.offer (Index + 1);  the             } the              the             if(Y > 0) { theQ.offer (index-1);  -             } the              the             if(X > 0) { theQ.offer (Index-cols); 94             } the              the             if(X < Rows-1) { theQ.offer (Index +cols); 98             } About         } -     }101}`
View Code

Solution 2:

The DFS solution is attached:

`1  Public voidDfsChar[] board,intIintj) {2         introws =board.length;3         intcols = board.length;4         5         //Out of bound or visited.6         if(I < 0 | | I >= rows | | J < 0 | | J >=cols) {7             return;8         }9         Ten         if(board[i][j]! = ' O ') { One             return; A         } -          -BOARD[I][J] = ' B '; the          -         //DFS the sorrounded regions. -DFS (board, i + 1, j); -DFS (board, i-1, j); +DFS (board, I, J + 1); -DFS (board, I, j-1); +}`
View Code

GITHUB:

Https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/bfs/Solve.java

Leetcode:surrounded Regions Problem Solving Report

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.