LeetCode: surrounded-regions (deep traversal application)

Topic description


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

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

X X X X
X X X X
X X X X
X O X X

Idea: What needs to be considered here is the O surrounded by X. If the traversal starts from each of the four sides, the depth traversal is performed. As long as all Os connected to the four sides are retained, the Os on the four sides are traversed, and the depth Traverse the Os connected to it and convert these Os to *, which is equivalent to marking;

Then change the remaining O to X and the remaining * to O, and the replacement can be achieved.

Full code:

public class SurroundRegion {
	public static void main(String[] args) {
		char [] [] chars = {{'O','X','X','X','X'}, {'O','X','O','O','X' }, {'O','X','X','X','X'}};
		for(int i=0; i<chars.length; ++i) {
	          for(int j=0; j<chars[i].length; ++j)
	             System.out.print(chars[i][j]);
	          System.out.println('\n');
		}
		System.out.println("*************************");
		SurroundRegion sur=new SurroundRegion();
		sur.tihuan(tanks);
		for(int i=0; i<chars.length; ++i) {
	          for(int j=0; j<chars[i].length; ++j)
	             System.out.print(chars[i][j]);
	          System.out.println('\n');//Display of newline
		}
	}
	
/*
* All O's connected to the four edges are retained, other O's become X's
* Traverse the Os on the four edges, and deeply traverse the Os connected to them, turning these Os into *
* Change the remaining O to X
* Change the remaining * to O
*/
	public int rowNum = 0;
	public int colNum = 0;
	public void tihuan(char[][] board) {
		if(board == null || board.length <= 0|| board[0].length <= 0){
			 return;
	    }
		rowNum = board.length;
		//Get the number of rows, when the length of the array name is directly, it is the dimension of the higher-level array
		colNum = board[0].length;
		// get the number of columns
		System.out.println(colNum);
		// After traversing the four edges, it is deeply traversed, as long as it is connected to the edge, it cannot be deleted
		for(int i = 0; i < colNum; i++){
			dfs(board, 0, i);
			dfs(board, rowNum-1, i);
		}
		for(int i = 0; i < rowNum; i++){
			dfs(board, i, 0);
			dfs(board, i, colNum-1);
		}
		for(int i = 0; i < rowNum; i++){
			for(int j = 0; j < colNum; j++){
				if(board[i][j] == 'O'){
					board[i][j] = 'X';
				}
			}
		}
		for(int i = 0; i < rowNum; i++){
			for(int j = 0; j < colNum; j++){
			    if(board[i][j] == '*'){
			       board[i][j] = 'O';
			    }
			}
		}
	}
	private void dfs(char[][] board, int row, int col) {
			    // TODO Auto-generated method stub
		if(board[row][col] == 'O'){
			 board[row][col] = '*';
			 if(row > 1){
			     dfs(board, row-1, col);
			 }
			 if(col > 1){
			     dfs(board, row, col-1);
			 }
			 if(row < rowNum-1){
			     dfs(board, row+1, col);
			 }
			 if(col < colNum-1){
			      dfs(board, row, col+1);
			 }
		}
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325978755&siteId=291194637