原题网址:https://leetcode.com/problems/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
方法:递归或者栈(但递归会溢出)。
public class Solution {
private boolean[][] check;
private boolean needCheck(char[][] board, boolean[][] checked, int i, int j) {
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return false;
if (checked[i][j] || board[i][j] == 'X') return false;
return true;
}
private void check(char[][] board, boolean[][] checked, int i, int j) {
if (!needCheck(board, checked, i, j)) return;
Stack<Position> stack = new Stack<>();
stack.push(new Position(i, j));
while (!stack.isEmpty()) {
Position position = stack.pop();
if (!needCheck(board, checked, position.x, position.y)) continue;
checked[position.x][position.y] = true;
if (needCheck(board, checked, position.x-1, position.y)) stack.push(new Position(position.x-1, position.y));
if (needCheck(board, checked, position.x+1, position.y)) stack.push(new Position(position.x+1, position.y));
if (needCheck(board, checked, position.x, position.y-1)) stack.push(new Position(position.x, position.y-1));
if (needCheck(board, checked, position.x, position.y+1)) stack.push(new Position(position.x, position.y+1));
}
}
public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) return;
boolean[][] checked = new boolean[board.length][board[0].length];
for(int i=0; i<board.length; i++) {
check(board, checked, i, 0);
check(board, checked, i, board[0].length-1);
}
for(int j=0; j<board[0].length; j++) {
check(board, checked, 0, j);
check(board, checked, board.length-1, j);
}
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[i].length; j++) {
if (checked[i][j]) board[i][j] = 'O';else board[i][j] = 'X';
}
}
}
}
class Position {
int x, y;
Position(int x, int y) {
this.x = x;
this.y = y;
}
}
另一种实现:
public class Solution {
private int[] dy = {-1, 1, 0, 0};
private int[] dx = {0, 0, -1, 1};
private void find(char[][] board, int y, int x, boolean[][] visit) {
visit[y][x] = true;
LinkedList<Position> list = new LinkedList<>();
list.add(new Position(y, x));
while (!list.isEmpty()) {
Position pos = list.remove();
for(int i=0; i<4; i++) {
int ny = pos.y + dy[i];
int nx = pos.x + dx[i];
if (ny < 0 || ny >= board.length || nx < 0 || nx >= board[ny].length) continue;
if (visit[ny][nx] || board[ny][nx] == 'X') continue;
visit[ny][nx] = true;
list.add(new Position(ny, nx));
}
}
}
public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) return;
boolean[][] visit = new boolean[board.length][board[0].length];
for(int i=0; i<board.length; i++) {
if (board[i][0] == 'O') find(board, i, 0, visit);
if (board[i][board[i].length-1] == 'O') find(board, i, board[i].length-1, visit);
}
for(int i=0; i<board[0].length; i++) {
if (board[0][i] == 'O') find(board, 0, i, visit);
if (board[board.length-1][i] == 'O') find(board, board.length-1, i, visit);
}
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[i].length; j++) {
if (board[i][j] == 'O' && !visit[i][j]) board[i][j] = 'X';
}
}
}
}
class Position {
int y, x;
Position(int y, int x) {
this.y = y;
this.x = x;
}
}