Surrounded Regions

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
核心的思想就是边上的O都不能改变,深度搜索与这样的点相邻的点,保持不变。

public void solve(char[][] board) {
	if(board == null || board.length==0) 
		return;
	int len1=board.length,len2=board[0].length;
	for(int i=0;i<len1;i++){
		if(board[i][0]=='O')
			merge(board,i,0);
		if(board[i][len2-1]=='O')
			merge(board,i,len2-1);
	}
	for(int i=0;i<len2;i++){
		if(board[0][i]=='O')
			merge(board,0,i);
		if(board[len1-1][i]=='O')
			merge(board,len1-1,i);
	}
	for(int i=0;i<len1;i++){
		for(int j=0;j<len2;j++){
			if(board[i][j]=='O')
				board[i][j]='X';
			else if(board[i][j]=='*')
				board[i][j]='O';
		}
	}
}
public void merge(char[][] board, int i, int j){
	if(i<0 || i>=board.length || j<0 || j>=board[0].length) 
		return;
	if(board[i][j] != 'O')
		return;
	board[i][j] = '*';
	merge(board, i-1, j);
	merge(board, i+1, j);
	merge(board, i, j-1);
	merge(board, i, j+1);
}
上面的代码当board很大时,就会出现java.lang.StackOverflowError的错误,所有

改用一个队列来存储邻边的O。

public class Solution {
    private static Queue<Integer> queue = null;
    private static char[][] board;
    private static int rows = 0;
    private static int cols = 0;

    public void solve(char[][] board) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if (board.length == 0 || board[0].length == 0) return;
        queue = new LinkedList<Integer>();
        board = board;
        rows = board.length;
        cols = board[0].length;
        for (int i = 0; i < rows; i++) { // **important**
            enqueue(i, 0);
            enqueue(i, cols - 1);
        }
        for (int j = 1; j < cols - 1; j++) { // **important**
            enqueue(0, j);
            enqueue(rows - 1, j);
        }
        while (!queue.isEmpty()) {
            int cur = queue.poll();
            int x = cur / cols,
                y = cur % cols;
            if (board[x][y] == 'O') {
                board[x][y] = 'D';
            }
            enqueue(x - 1, y);
            enqueue(x + 1, y);
            enqueue(x, y - 1);
            enqueue(x, y + 1);
        }
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (board[i][j] == 'D') board[i][j] = 'O';
                else if (board[i][j] == 'O') board[i][j] = 'X';
            }
        }
        queue = null;
        board = null;
        rows = 0;
        cols = 0;
    }
    public static void enqueue(int x, int y) {
        if (x >= 0 && x < rows && y >= 0 && y < cols && board[x][y] == 'O'){  
            queue.offer(x * cols + y);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值