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 {
public void solve(char[][] board) {
if (board==null || board.length<1)
return;
boolean[][] visit = new boolean[board.length][board[0].length];
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] && (i==0||j==0||i==board.length-1||j==board[j].length-1)) {
Queue<Point> queue = new LinkedList<Point>();
queue.add(new Point(i, j));
while (!queue.isEmpty()) {
Point point = queue.poll();
visit[point.i][point.j] = true;//Notice here
if (point.j>0 && board[point.i][point.j-1]=='O' && !visit[point.i][point.j-1]) {
queue.add(new Point(point.i, point.j-1));//Notice here
}
if (point.j<board[point.i].length-1 && board[point.i][point.j+1]=='O' && !visit[point.i][point.j+1]) {
queue.add(new Point(point.i, point.j+1));//Notice here
}
if (point.i>0 && board[point.i-1][point.j]=='O' && !visit[point.i-1][point.j]) {
queue.add(new Point(point.i-1, point.j));//Notice here
}
if (point.i<board.length-1 && board[point.i+1][point.j]=='O' && !visit[point.i+1][point.j]) {
queue.add(new Point(point.i+1, point.j));//Notice here
}
}
}
for (int i=0; i<board.length; i++)
for (int j=0; j<board[i].length; j++)
if (visit[i][j])
board[i][j] = 'O';
else
board[i][j] = 'X';
}
}
class Point {
public int i;
public int j;
public Point(int ii, int jj) {
this.i = ii;
this.j = jj;
}
}
很顺利的通过了Judge Small,但是运行Judge Large时,却 Time Limit Exceeded,所谓large test case也只是一个20*20的矩阵,这么小的数据集,运行超时,这是为什么 呢?请注意上面代码里有注释的那几行,这样写会导致有些节点被多次添加至队列,人为增加了队列长度跟搜索状态,具体是为什么,请读者自己思考。
修改代码以后:
public class Solution {
public void solve(char[][] board) {
if (board==null || board.length<1)
return;
boolean[][] visit = new boolean[board.length][board[0].length];
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] && (i==0||j==0||i==board.length-1||j==board[j].length-1)) {
Queue<Point> queue = new LinkedList<Point>();
queue.add(new Point(i, j));
visit[i][j] = true;
while (!queue.isEmpty()) {
Point point = queue.poll();
if (point.j>0 && board[point.i][point.j-1]=='O' && !visit[point.i][point.j-1]) {
queue.add(new Point(point.i, point.j-1));
visit[point.i][point.j-1] = true; //add code here
}
if (point.j<board[point.i].length-1 && board[point.i][point.j+1]=='O' && !visit[point.i][point.j+1]) {
queue.add(new Point(point.i, point.j+1));
visit[point.i][point.j+1] = true;//add code here
}
if (point.i>0 && board[point.i-1][point.j]=='O' && !visit[point.i-1][point.j]) {
queue.add(new Point(point.i-1, point.j));
visit[point.i-1][point.j] = true;//add code here
}
if (point.i<board.length-1 && board[point.i+1][point.j]=='O' && !visit[point.i+1][point.j]) {
queue.add(new Point(point.i+1, point.j));
visit[point.i+1][point.j] = true;//add code here
}
}
}
for (int i=0; i<board.length; i++)
for (int j=0; j<board[i].length; j++)
if (visit[i][j])
board[i][j] = 'O';
else
board[i][j] = 'X';
}
}
class Point {
public int i;
public int j;
public Point(int ii, int jj) {
this.i = ii;
this.j = jj;
}
}
顺利通过Large Judge