您的位置:首页 > 其它

[LeetCode#130]Surrounded Regions

2015-08-27 06:03 267 查看
Problem:

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

Analysis:

Since we have already solved the problem of isolated islands, this problem is very easy to think about.
Instant idea:
We begain from each 'O' to merge, once we encounter a 'O' at the broder, all elements this DFS process would not be captured. It seems reasonable, but we have four directions to streach, to pass the state of whether a element meet the border could be a nightmare for implementation.

Think the problem through other way:
Since we decide wether a region could be captured based on whether there is a island on the border. Whey not we start from each island at border, then try to reach other islands connected with it. After the process, the islands that were not connected would be captured.
Step1 : start from border elements.
for (int i = 0; i < row_len; i++) {
if (board[i][0] == 'O')
merge(board, i, 0);
if (board[i][column_len-1] == 'O')
merge(board, i, column_len-1);

}

Step2 : use DFS to reach all elementes in the same region, and tag them with '#' tag.
private void merge(char[][] board, int i, int j) {
if (i < 0 || j < 0 || i > board.length - 1 || j > board[0].length -1)
return;
if (board[i][j] == 'X' || board[i][j] == '#') return;
board[i][j] = '#';
merge(board, i-1, j);
merge(board, i+1, j);
merge(board, i, j-1);
merge(board, i, j+1);
}

Step3 : check all elements on the board, if a island was not tagged, it should be caputured.
private void merge(char[][] board, int i, int j) {
if (i < 0 || j < 0 || i > board.length - 1 || j > board[0].length -1)
return;
if (board[i][j] == 'X' || board[i][j] == '#') return;
board[i][j] = '#';
merge(board, i-1, j);
merge(board, i+1, j);
merge(board, i, j-1);
merge(board, i, j+1);
}

Solution 1: (DFS)
public class Solution {
public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0)
return;
int row_len = board.length;
int column_len = board[0].length;
for (int i = 0; i < row_len; i++) {
if (board[i][0] == 'O')
merge(board, i, 0);
if (board[i][column_len-1] == 'O')
merge(board, i, column_len-1);

}
for (int j = 0; j < column_len; j++) {
if (board[0][j] == 'O')
merge(board, 0, j);
if (board[row_len-1][j] == 'O')
merge(board, row_len-1, j);
}
for (int i = 0; i < row_len; i++) {
for (int j = 0; j < column_len; j++) {
if (board[i][j] == '#')
board[i][j] = 'O';
else
board[i][j] = 'X';
}
}
}

private void merge(char[][] board, int i, int j) {
if (i < 0 || j < 0 || i > board.length - 1 || j > board[0].length -1)
return;
if (board[i][j] == 'X' || board[i][j] == '#') return;
board[i][j] = '#';
merge(board, i-1, j);
merge(board, i+1, j);
merge(board, i, j-1);
merge(board, i, j+1);
}
}

Even though the above solution is right, it could easily incure stack overflow prolems.
We should try to replace the DFS method to BFS method to avoid recursive calls.

Skill:
Rather than tag a island, and then use dfs search along one direction.

private void merge(char[][] board, int i, int j) {
if (i < 0 || j < 0 || i > board.length - 1 || j > board[0].length -1)
return;
if (board[i][j] == 'X' || board[i][j] == '#') return;
board[i][j] = '#';
merge(board, i-1, j);
...
}

We could separte the tag process and search process to avoid recursive.
The bridge between those twp steps are a queue.
step1: tag a island  => put the island into the queue
private void fill(char[][] board, int i, int j, Queue<Integer> queue) {
int m = board.length;
int n = board[0].length;
if (i < 0 || j < 0 || i >= m || j >= n || board[i][j] != 'O')
return;
queue.offer(i*n + j);
board[i][j] = '#';
}

step2: dequeue a island from the queue => tag four islands around it.
private void bfs(char[][] board, int i, int j, Queue<Integer> queue) {
int n = board[0].length;
//the start element of BFS search
fill(board, i, j, queue);
while (!queue.isEmpty()) {
int index = queue.poll();
int x = index / n;
int y = index % n;
fill(board, x, y-1, queue);
fill(board, x, y+1, queue);
fill(board, x-1, y, queue);
fill(board, x+1, y, queue);
}
}

Thus we could avoid the recursive of tagging a island and serach a direction of the island. all directions' island was put into the queue at the same call.

Note: We should still use the checking condition we used in DFS, only island == '0' could be tagged. and the direction around it should be searched.
if (i < 0 || j < 0 || i >= m || j >= n || board[i][j] != 'O')
return;
Key: board[i][j] != 'O' at here also means
iff boad[i][j] == '#', the island has already been tagged, it should not visited again. This case is very common in BFS searching.


Solution:

public class Solution {
public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0)
return;
Queue<Integer> queue = new LinkedList<Integer> ();
int row_len = board.length;
int column_len = board[0].length;

for (int i = 0; i < row_len; i++) {
if (board[i][0] == 'O')
bfs(board, i, 0, queue);
if (board[i][column_len-1] == 'O')
bfs(board, i, column_len-1, queue);

}
for (int j = 0; j < column_len; j++) {
if (board[0][j] == 'O')
bfs(board, 0, j, queue);
if (board[row_len-1][j] == 'O')
bfs(board, row_len-1, j, queue);
}
for (int i = 0; i < row_len; i++) {
for (int j = 0; j < column_len; j++) {
if (board[i][j] == '#')
board[i][j] = 'O';
else
board[i][j] = 'X';
}
}
}

//separate the process of forking!!!
private void bfs(char[][] board, int i, int j, Queue<Integer> queue) {
int n = board[0].length;
//the start element of BFS search
fill(board, i, j, queue);
while (!queue.isEmpty()) {
int index = queue.poll();
int x = index / n;
int y = index % n;
fill(board, x, y-1, queue);
fill(board, x, y+1, queue);
fill(board, x-1, y, queue);
fill(board, x+1, y, queue);
}
}

private void fill(char[][] board, int i, int j, Queue<Integer> queue) {
int m = board.length;
int n = board[0].length;
if (i < 0 || j < 0 || i >= m || j >= n || board[i][j] != 'O')
return;
queue.offer(i*n + j);
board[i][j] = '#';
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: