Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region.
思路
本来想用dfs做,代码感觉比bfs感觉简单多了,结果做出来是stackoverflow的,要加上限制条件,才能跑过。遇到这种图问题还是要先确定一下图的大小,再确定用BFS还是DFS
代码
public void solve(char[][] board) {
//corner case
if(board == null || board.length <= 1 || board[0] == null || board[0].length <= 1) return;
int rows = board.length, cols = board[0].length;
//flip all adjacent boarder O into Y, the first col
for(int i = 0; i < rows; i++){
if(board[i][0] == 'O'){
helper(board, rows, cols, i, 0);
}
}
//flip all adjacent boarder O into Y, the first row
for(int j = 0; j < cols; j++){
if(board[0][j] == 'O'){
helper(board, rows, cols, 0, j);
}
}
//flip all adjacent boarder O into Y, the last col
for(int i = 0; i < rows; i++){
if(board[i][cols - 1] == 'O'){
helper(board, rows, cols, i, cols - 1);
}
}
//flip all adjacent boarder O into Y, the last row
for(int j = 0; j < cols; j++){
if(board[rows - 1][j] == 'O'){
helper(board, rows, cols, rows - 1, j);
}
}
//flip all 'Y' into 'O', flip all 'O' into 'X'
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(board[i][j] == 'Y'){
board[i][j] = 'O';
}
else if(board[i][j] =='O'){
board[i][j] = 'X';
}
}
}
}
private void helper(char[][] board, int rows, int cols, int i, int j){
//base case
if(i < 0 || i >= rows || j < 0 || j >= cols || board[i][j] != 'O'){
return;
}
board[i][j] = 'Y';
//go up
if(i < rows - 2) helper(board, rows, cols, i + 1, j);
//go down
if(i > 1) helper(board, rows, cols, i - 1, j);
//go left
if(j > 1) helper(board, rows, cols, i, j - 1);
//go right
if(j < cols - 2) helper(board, rows, cols, i, j + 1);
}
BFS
思路
把四周的是‘O’的点全部加入queue, 把与它相连的'O'再全部加入queue,翻转成'#'
最后再全部遍历一遍,把’#'变成’O‘,’O‘变成’X'
代码
class Point{
//fields
private int x; //the row of the Point
private int y; //the col of the Point
//methods
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
public void solve(char[][] board) {
//corner case
if(board == null || board.length <= 2 || board[0] == null || board[0].length <= 2) return;
int rows = board.length, cols = board[0].length;
Queue<Point> queue = new LinkedList<Point>();
for(int i = 0; i < rows; i++){
if(board[i][0] == 'O'){
queue.offer(new Point(i, 0));
board[i][0] = '#';
}
if(board[i][cols - 1] == 'O' ){
queue.offer(new Point(i, cols - 1));
board[i][cols - 1] = '#';
}
}
for(int j = 0; j < cols; j++){
if(board[0][j] == 'O'){
queue.offer(new Point(0, j));
board[0][j] = '#';
}
if(board[rows - 1][j] == 'O'){
queue.offer(new Point(rows - 1, j));
board[rows - 1][j] = '#';
}
}
int[][] directions = new int[][]{{-1,0},{1,0},{0,1},{0,-1}};
while(!queue.isEmpty()){
Point cur = queue.poll();
for(int k = 0; k < 4; k++){
int row = cur.x + directions[k][0];
int col = cur.y + directions[k][1];
if(row >= 0 && row < rows && col >= 0 && col < cols && board[row][col] == 'O'){
board[row][col] = '#';
queue.offer(new Point(row, col));
}
}
}
//flip
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(board[i][j] == '#'){
board[i][j] = 'O';
}
else if(board[i][j] == 'O'){
board[i][j] = 'X';
}
}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。