LeetCode - 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 {
    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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值