[LeetCode] 130. 被围绕的区域

题目不难理解,可以将问题转化成判断O构成的连通区域,是否有位于边界的块。

直接用朴素的BFS解决问题。BFS时要向上下左右四个方向都走,走过的块记录的list中,走的同时判断如果有位于边界的块,flag记为true

最后BFS完成后,如果是 false,那么就可以把list中记录的块置为X了;如果flag = true,先把list中记录的块置为S,这样做是为了标记该’O’区域的连通区域已经遍历过了

然后继续遍历board,寻找下一个连通区域

手生,没1A,两次WA后才过,哎

class Solution {
    public void solve(char[][] board) {
        int m = board.length;
        if (m == 0) return;
        int n = board[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'O') {
                    bfs(board, i, j, m, n);
                }
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'S') {
                    board[i][j] = 'O';
                }
            }
        }
    }

    private void bfs(char[][] board, int i, int j, int m, int n) {
        List<List<Integer>> list = new ArrayList();
        list.add(buildPair(i,j));

        Queue<List<Integer>> queue = new LinkedList<List<Integer>>();
        queue.offer(list.get(0));

        boolean flag = false;
        if (i==0||j==0) flag = true;

        while (!queue.isEmpty()) {
            List<Integer> cur = queue.poll();

            int p = cur.get(0);
            int q = cur.get(1);
            int np = p + 1;
            int nq = q + 1;
            if (p+1>=m) flag = true; 
            else {
                if (board[np][q] == 'O') {
                    List<Integer> pair = buildPair(np,q);
                    list.add(pair);
                    queue.offer(pair);
                    board[np][q] = 'V';
                }
            }
            if (q+1>=n) flag = true;
            else {
                if (board[p][nq] == 'O') {
                    List<Integer> pair = buildPair(p,nq);
                    list.add(pair);
                    queue.offer(pair);
                    board[p][nq] = 'V';
                }
            }
            int lp = p - 1;
            int lq = q - 1;
            if (lp < 0) flag = true;
            else {
                if (board[lp][q] == 'O') {
                    List<Integer> pair = buildPair(lp,q);
                    list.add(pair);
                    queue.offer(pair);
                    board[lp][q] = 'V';
                }
            }
            if (lq < 0) flag = true;
            else {
                if (board[p][lq] == 'O') {
                    List<Integer> pair = buildPair(p,lq);
                    list.add(pair);
                    queue.offer(pair);
                    board[p][lq] = 'V';
                }
            }
        }

        if (flag) {
            print(board, list, 'S');
        } else {
            print(board, list, 'X');
        }
    }

    private void print(char[][] board, List<List<Integer>> list, char ch) {
        for (int i = 0;i<list.size();i++) {
            List<Integer> pair = list.get(i);
            board[pair.get(0)][pair.get(1)] = ch;
        }
    }

    private List<Integer> buildPair(int i, int j) {
        List<Integer> pair = new ArrayList();
        pair.add(i);
        pair.add(j);

        return pair;
    }
}
posted @ 2021-06-06 23:58  ACBingo  阅读(36)  评论(0编辑  收藏  举报