题目
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’结点。可以使用DFS遍历,但是会出现栈溢出的问题
//方法一:使用DFS:栈溢出。 // private int lenX; // private int lenY; // // private Queue<Integer> queue = new LinkedList<Integer>(); // private boolean isRightIndex(int i, int j) { // if (i < 0 || i >= lenX || j < 0 || j >= lenY) { // return false; // } // return true; // } // // // private void alterAround(int i, int j, boolean[][] status, char[][] board) { // if (isRightIndex(i - 1, j) && board[i - 1][j] == 'O' && status[i - 1][j] == false) { // status[i - 1][j] = true; // alterAround(i - 1, j, status, board); // } // if (isRightIndex(i + 1, j) && board[i + 1][j] == 'O' && status[i + 1][j] == false) { // status[i + 1][j] = true; // alterAround(i + 1, j, status, board); // } // if (isRightIndex(i, j - 1) && board[i][j - 1] == 'O' && status[i][j - 1] == false) { // status[i][j - 1] = true; // alterAround(i, j - 1, status, board); // } // if (isRightIndex(i, j + 1) && board[i][j + 1] == 'O' && status[i][j + 1] == false) { // status[i][j + 1] = true; // alterAround(i, j + 1, status, board); // } // } // // public void solve(char[][] board) { // if (board != null && board.length != 0 && board.length != 1 && board.length != 2) { // this.lenX = board.length; // this.lenY = board[0].length; // boolean[][] status = new boolean[lenX][lenY]; // int k = 0; // for (int j = k; j < lenY - k; j++) { // if (board[k][j] == 'O') { // status[k][j] = true; // alterAround(k, j, status, board); // } // } // // for (int i = k + 1; i < lenX - k; i++) { // if (board[i][lenY - 1 - k] == 'O') { // status[i][lenY - 1 - k] = true; // alterAround(i, lenY - 1 - k, status, board); // } // } // // for (int j = lenY - (k + 1); j > k; j--) { // if (board[lenX - 1 - k ][j - 1] == 'O') { // status[lenX - 1 - k ][j - 1] = true; // alterAround(lenX - 1 - k, j - 1,status, board); // } // } // // for (int i = lenX - (k + 2); i > k; i--) { // if (board[i][k] == 'O') { // status[i][k] = true; // // alterAround(i, k, status, board); // } // } // // for (int i = 1; i < lenX - 1; i++) { // for (int j = 1; j < lenY -1; j++) { // if (board[i][j] == 'O' && status[i][j] == false) { // board[i][j] = 'X'; // } // } // } // } // }
方法二
使用BFS遍历
//方法二:使用BFS private int lenX; private int lenY; private Queue<Integer> queue = new LinkedList<Integer>(); private boolean isRightIndex(int i, int j) { if (i < 0 || i >= lenX || j < 0 || j >= lenY) { return false; } return true; } public void traversal(int x, int y, boolean[][] status, char[][] board) { if (isRightIndex(x, y) && board[x][y] == 'O' && status[x][y] == false) { status[x][y] = true; queue.offer(x * lenY + y); } while(!queue.isEmpty()) { int index = queue.poll(); int i = index / lenY; int j = index % lenY; if (isRightIndex(i - 1, j) && board[i - 1][j] == 'O' && status[i - 1][j] == false) { status[i - 1][j] = true; queue.offer((i - 1) * lenY + j); } if (isRightIndex(i + 1, j) && board[i + 1][j] == 'O' && status[i + 1][j] == false) { status[i + 1][j] = true; queue.offer((i + 1) * lenY + j); } if (isRightIndex(i, j - 1) && board[i][j - 1] == 'O' && status[i][j - 1] == false) { status[i][j - 1] = true; queue.offer(i * lenY + j - 1); } if (isRightIndex(i, j + 1) && board[i][j + 1] == 'O' && status[i][j + 1] == false) { status[i][j + 1] = true; queue.offer(i * lenY + j + 1); } } } public void solve(char[][] board) { if (board != null && board.length != 0 && board.length != 1 && board.length != 2) { this.lenX = board.length; this.lenY = board[0].length; boolean[][] status = new boolean[lenX][lenY]; int k = 0; for (int j = k; j < lenY - k; j++) { if (board[k][j] == 'O') { traversal(k, j, status, board); } } for (int i = k + 1; i < lenX - k; i++) { if (board[i][lenY - 1 - k] == 'O') { traversal(i, lenY - 1 - k, status, board); } } for (int j = lenY - (k + 1); j > k; j--) { if (board[lenX - 1 - k ][j - 1] == 'O') { traversal(lenX - 1 - k, j - 1,status, board); } } for (int i = lenX - (k + 2); i > k; i--) { if (board[i][k] == 'O') { traversal(i, k, status, board); } } for (int i = 1; i < lenX - 1; i++) { for (int j = 1; j < lenY -1; j++) { if (board[i][j] == 'O' && status[i][j] == false) { board[i][j] = 'X'; } } } } }