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连通的那些O,用特殊字符#标记出来,然后遍历矩阵把O赋为X,把#赋值为O。而搜索与边缘的O连通的O的方法,可以用DFS或者BFS,尝试从边缘每一个O开始搜索。但是这题用DFS递归写法会超时,因为测试用例有一个很大的矩阵,会导致递归栈溢出。下面AC Code用的是BFS,借助于队列实现的迭代式的BFS。另外队列保存的是矩阵中的数的一维index,要注意换算方法。这个算法在图形学中称为Flood fill算法。时间复杂度为O(mn),空间复杂度为O(m+n)。
AC Code
public class Solution {
//use bfs search(in an iterative manner with queue)
public void bfs(char[][] board, int i, int j, int m, int n){
board[i][j] = '#';
//10:47
//use queue to store all the adjacent node using sequcial index
LinkedList<Integer> queue = new LinkedList<Integer>();
int index = i*n + j;
queue.add(index);
while(!queue.isEmpty()){
int cur = queue.poll();
int row = cur / n;
int col = cur % n;
if(row+1 < m && board[row+1][col] == 'O'){
queue.add((row+1)*n + col);
board[row+1][col] = '#';
}
if(row-1 >= 0 && board[row-1][col] == 'O'){
queue.add((row-1)*n + col);
board[row-1][col] = '#';
}
if(col+1 < n && board[row][col+1] == 'O'){
queue.add(row*n + col + 1);
board[row][col+1] = '#';
}
if(col-1 >= 0 && board[row][col-1] == 'O'){
queue.add(row*n + col - 1);
board[row][col-1] = '#';
}
}
}
public void solve(char[][] board) {
if(board == null) return;
int m = board.length;
if(m <= 1) return;
int n = board[0].length;
for(int i = 0; i < m; i++){
if(board[i][0] == 'O'){
bfs(board, i, 0, m, n);
}
if(board[i][n-1] == 'O'){
bfs(board, i, n-1, m, n);
}
}
for(int j = 0; j < n; j++){
if(board[0][j] == 'O'){
bfs(board, 0, j, m, n);
}
if(board[m-1][j] == 'O'){
bfs(board, m-1, j, m, n);
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 'O'){
board[i][j] = 'X';
} else if(board[i][j] == '#'){
board[i][j] = 'O';
}
}
}
}
//Use dfs search. If the input matrix is very large, it will stack overflow
//So this problem should be solved by dfs in a iterative manner(with stack) or bfs in a iterative manner(with queue)
//Could not be solved by dfs in recurcive way
//0951
/*public void dfs(char[][] board, int i, int j, int m, int n){
board[i][j] = '#';
if(i+1 < m && board[i+1][j] == 'O'){
dfs(board, i+1, j, m, n);
}
if(i-1 >= 0 && board[i-1][j] == 'O'){
dfs(board, i-1, j, m, n);
}
if(j+1 < n && board[i][j+1] == 'O'){
dfs(board, i, j+1, m, n);
}
if(j-1 >= 0 && board[i][j-1] == 'O'){
dfs(board, i, j-1, m, n);
}
}
public void solve(char[][] board) {
if(board == null) return;
int m = board.length;
if(m <= 1) return;
int n = board[0].length;
for(int i = 0; i < m; i++){
if(board[i][0] == 'O'){
dfs(board, i, 0, m, n);
}
if(board[i][n-1] == 'O'){
dfs(board, i, n-1, m, n);
}
}
for(int j = 0; j < n; j++){
if(board[0][j] == 'O'){
dfs(board, 0, j, m, n);
}
if(board[m-1][j] == 'O'){
dfs(board, m-1, j, m, n);
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 'O'){
board[i][j] = 'X';
} else if(board[i][j] == '#'){
board[i][j] = 'O';
}
}
}
}*/
//10:02
}