Surrounded regions
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
https://leetcode.com/problems/surrounded-regions/
Figured out is actually very simple, but I was tossing for a long time ...
The first round to find the periphery of all o, these points will certainly not be replaced by X.
Then Dfs, the points connected to these points will not be x.
Mark the dots that are not X, and finally replace all o with X, and replace all S with O.
When I first tried to do it, I first cached the points that needed to be labeled, and the breath was marked X, and the proper mle.
The second attempt to use BFS, first labeled X, if not go back to change to O, recursion too deep stack burst ... That's a bad count.
1 /**2 * @param {character[][]} board3 * @return {void} does not return anything, modify board In-place instead.4 */5 varSolve =function(board) {6 varm = board.length, N, I, j, queue = [];7 for(i = 0; i < m; i++){8n = board[0].length;9 forj = 0; J < N; j + +){Ten if(Board[i][j] = = = ' O ' && (i = = 0 | | i = = M-1 | | j = = 0 | | j = = = N-1)){ One Queue.push ({row:i, col:j}); A } - } - } the BFS (queue); - for(i = 0; i < m; i + +)){ - for(j = 0; J < N; j + +)){ - if(Board[i][j] = = = ' O '){ +BOARD[I][J] = ' X '; -}Else if(Board[i][j] = = = ' S '){ +BOARD[I][J] = ' O '; A } at } - } - - functionBFS (queue) { - varI, J; - while(Queue.length > 0){ in vartop =Queue.pop (); -i = top.row, j =Top.col; to if(Board[i][j] = = = ' S '){ + Continue; - } theBOARD[I][J] = ' S '; * if(Board[i + 1] && board[i + 1][j] && board[i + 1][j] = = = ' O '){ $Queue.push ({row:i + 1, col:j});Panax Notoginseng } - if(Board[i-1] && board[i-1][j] && board[i-1][j] = = = ' O '){ theQueue.push ({row:i-1, col:j}); + } A if(Board[i][j + 1] && board[i][j + 1] = = = ' O '){ theQueue.push ({row:i, col:j + 1}); + } - if(Board[i][j-1] && board[i][j-1] = = = ' O '){ $Queue.push ({row:i, col:j-1}); $ } - } - } the};
TLE:
1 /**2 * @param {character[][]} board3 * @return {void} does not return anything, modify board In-place instead.4 */5 varSolvetle =function(board) {6 varm =board.length, N, I, J;7 for(i = 0; i < m; i++){8n = board[0].length;9 forj = 0; J < N; j + +){Ten if(Board[i][j] = = = ' O '){ One BFS ([{row:i, col:j}]); A } - } - } the for(i = 0; i < m; i++){ - forj = 0; J < N; j + +){ - if(Board[i][j] = = = ' A '){ -BOARD[I][J] = ' X '; + } - } + } A at functionBFS (queue) { - varLen =queue.length, Top, I, J, Res; - while(len--){ -top =Queue.pop (); -i = Top.row; j =Top.col; - if(!board[i] | |!Board[i][j]) { in return false; - } to if(Board[i][j] = = = ' O '){ +Queue.push ({row:i + 1, col:j}); -Queue.push ({row:i-1, col:j}); theQueue.push ({row:i, col:j + 1}); *Queue.push ({row:i, col:j-1}); $ }Panax Notoginseng } - if(queue.length!== 0){ theBOARD[I][J] = ' A '; +res =BFS (queue); A if(!Res) { theBOARD[I][J] = ' O '; + return false; - } $ } $ return true; - } -};
MLE is too ugly to write ...
[Leetcode] [JavaScript] Surrounded regions