Catalogue
- Directory
- Objective
- Topic
- Thinking 1_dfs
- Thinking 2_bfs
Preface
Recently these two days in order to solve the Android ROM adaptation of an underlying problem, every day up to 2 points, resulting in the original Leetcode brush project has been affected. Last night finally stayed up to fix, add a leetcode problem solving report.
Topics
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
Thinking 1_dfs
This topic gives the hint is to use breadth First search (BFS), but I suppress a half a day without thinking, because usually I usually use BFS to solve some of the maze of problems.
So, I still think of a change of mind, since BFS does not, that is DFS (depth-first search). Ideas are as follows:
- If there is an ' O ' in the outermost layer, it must not be surrounded by ' X '. At this point you need to traverse the outermost layer of the board two-dimensional array, which requires 4 for loops.
- Each time the outermost traversal process, if found to have ' o ', then we can try to go up and down the four direction of the Traverse, if again found ' O ', then the current ' o ' is not surrounded by ' X ', the ' o ' is converted to ' K '. The DFS process.
- Once the 4 traversal is complete, you need to traverse the board array again. Array elements that are not ' K ' should be surrounded by ' x ', so the unification is changed to ' X '. Do not forget to change back to ' O ' for ' K '.
The DFS code is as follows:
Public class solution { Private Static int[] Directions = {{0,1}, {0, -1}, {1,0}, {-1,0}}; Public Static void Solve(Char[] board) {introw = Board.length;if(Row <2) {return; }intColumn = board[0].length;if(Column <2) {return; } for(intj =0; J < board[0].length; J + +) {if(board[0][J] = =' O ') {Dfsboard (board,0, j); } } for(inti =1; i < board.length; i + +) {if(Board[i][column-1] ==' O ') {Dfsboard (board, I, column-1); } } for(intj = Column-2; J >=0; J--) {if(Board[row-1][J] = =' O ') {Dfsboard (board, Row-1, j); } } for(inti = row-2; I >=0; I--) {if(board[i][0] ==' O ') {Dfsboard (board, I,0); } } for(inti =0; i < row; i + +) { for(intj =0; J < column; J + +) {Board[i][j] = board[i][j] = =' K '?' O ':' X '; } } }Private Static void Dfsboard(Char[] board,intXintY) {Board[x][y] =' K '; for(inti =0; i < directions.length; i + +) {intnew_x = x + directions[i][0];intnew_y = y + directions[i][1];if(New_x <0|| new_x >= Board.length | | New_y <0|| New_y >= board[0].length | | BOARD[NEW_X][NEW_Y]! =' O ') {Continue; } dfsboard (board, new_x, new_y); } }}
Originally thought the result will be tle, did not expect unexpectedly, the result is runtime Error, recursive stack was blown off.
Thinking 2_bfs
DFS explosion to fully explain the problem of the tip or very useful, you must use BFS AH. However, the Dfs method above provides us with a good idea. The reason for the explosion is: the DFS stack depth is not enough, then we will directly change the DFS traversal here to BFS.
Where the ideas change:
- Each time the outermost traversal, if there is an ' o ', then we can try to go up and down the four direction of the Traverse, if again found ' O ', the current node into the queue. The ' O ' in the queue is also not surrounded by ' X ', which converts the ' o ' into ' K '. (PS: In fact, the stack implementation is changed to the queue implementation)
Directly on the AC code:
Public class solution { Private Static class boardpoint { Private intx, y; Public Boardpoint(intXintY) { This. x = x; This. y = y; } Public int GetX() {returnX } Public int GetY() {returnY } }Private Static int[] Directions = {{1,0}, { -1,0}, {0,1}, {0, -1} }; Public Static void Solve(Char[] board) {introw = Board.length;if(Row <2) {return; }intColumn = board[0].length;if(Column <2) {return; } for(intj =0; J < board[0].length; J + +) {if(board[0][J] = =' O ') {Bfsboard (board,0, J, row, column); } } for(inti =1; i < board.length; i++) {if(Board[i][column-1] ==' O ') {Bfsboard (board, I, column-1, row, column); } } for(intj = Column-2; J >=0; j--) {if(Board[row-1][J] = =' O ') {Bfsboard (board, Row-1, J, row, column); } } for(inti = row-2; I >=0; i--) {if(board[i][0] ==' O ') {Bfsboard (board, I,0, row, column); } } for(inti =0; i < row; i++) { for(intj =0; J < column; J + +) {Board[i][j] = board[i][j] = =' K '?' O ':' X '; } } }Private Static void Bfsboard(Char[] board,intXintYintRowintCol) {arraydeque<boardpoint> queue =NewArraydeque<boardpoint> (); Queue.push (NewBoardpoint (x, y)); while(!queue.isempty ()) {Boardpoint point = Queue.poll (); Board[point.getx ()][point.gety ()] =' K '; for(inti =0; i < directions.length; i++) {intnew_x = Point.getx () + directions[i][0];intNew_y = point.gety () + directions[i][1];if(new_x >=0&& new_x < row && new_y >=0&& new_y < col && board[new_x][new_y] = =' O ') {Queue.push (NewBoardpoint (new_x, new_y)); } } } }}
[Leetcode] Surrounded regions, problem solving report