Leetcode: surrounded regions [1, 130]

Source: Internet
Author: User
[Question]

Given a 2D Board containing‘X‘And‘O‘, Capture all regions surrounded‘X‘.

A region is captured by flipping all‘O‘S‘X‘S in that surrounded region.

For example,

X X X XX O O XX X O XX O X X

After running your function, the Board shoshould be:

X X X XX X X XX X X XX O X X


Question]

Given a two-dimensional matrix filled by 'X' and 'O', this question requires replacing those 'O' surrounded by 'X' with 'X '. Note that the surrounding area is completely surrounded. If a 'O' area touches the border of the matrix, this area is not surrounded by 'x.


[Idea]

We only need to replace all the 'O' areas that reach the boundary with another letter, such as 'M '. The remaining 'O' area in the matrix must be surrounded.
Then, replace 'O' with 'X' in the scan matrix and replace 'M' with 'O '.

Area traversal, using DFS or BFS
DFS uses recursion, and it is easy to time out when the matrix is large.
Therefore, this question uses BFS


[Code]
Class solution {public: void o2m (vector <char> & board, int I, Int J) {// starts from Board [I] [J, traverse the 'O' area and replace 'O' with 'M' int rows = board. size (); int Cols = Board [0]. size (); queue <int> QE; QE. push (I * Cols + J); Board [I] [J] = 'M'; while (! Qe. empty () {int Pos = QE. front (); QE. pop (); I = POS/Cols; j = POS % Cols; // determine the top if (I-1> = 0 & Board [I-1] [J] = 'O') {board [I-1] [J] = 'M '; // note that when you add an adjacent element to the queue, You need to mark it as an access. Otherwise, when you determine the adjacent location of the 'O' of other nodes, it is very likely that this node is added to the queue. Causes an endless loop. Qe. push (I-1) * Cols + J);} // judge the lower if (I + 1 <rows & Board [I + 1] [J] = 'O ') {board [I + 1] [J] = 'M'; QE. push (I + 1) * Cols + J);} // determine the left side if (J-1> = 0 & Board [I] [J-1] = 'O ') {board [I] [J-1] = 'M'; QE. push (I * Cols + J-1);} // determine the right side if (J + 1 <Cols & Board [I] [J + 1] = 'O ') {board [I] [J + 1] = 'M'; QE. push (I * Cols + J + 1) ;}} void solve (vector <char> & board) {int rows = board. size (); If (rows = 0) return; int Cols = Board [0]. size (); If (Cols = 0) return; // Replace the 'O' area of the edge with 'M' // above for (Int J = 0; j <Cols; j ++) {If (Board [0] [J] = 'O') o2m (board, 0, J );} // bottom for (Int J = 0; j <Cols; j ++) {If (Board [rows-1] [J] = 'O') o2m (board, rows-1, J);} // left for (INT I = 0; I <rows; I ++) {If (Board [I] [0] = 'O') o2m (board, I, 0);} // right for (INT I = 0; I <rows; I ++) {If (Board [I] [Cols-1] = 'O') o2m (board, I, cols-1);} // scan matrix, replace O with X and M with O for (INT I = 0; I <rows; I ++) {for (Int J = 0; j <Cols; j ++) {If (Board [I] [J] = 'O') Board [I] [J] = 'X '; else if (Board [I] [J] = 'M') Board [I] [J] = 'O ';}}}};


Contact Us

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.