130. 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.

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

矩阵的bfs, 套路一致,从外向内, 很easy

构造类, 遍历矩阵建立图

bfs要点在于如何建图, 是否建类, 建比较器, 建方向容器, 建走过的路的存储器(数组, 或者set, list) 如何遍历(堆不空?), 遍历到内部的点时判断是否符合题意(边界, 走过), 再考虑题意, 判断当前的点是否符合题意来加入结果的容器 或结果值, 将当前的点加入堆中是否要改变值啊什么的, 关键在于建立的是什么图. 里面的点是怎么个意思, 都是题意的转化

class Cell {
    int x, y;
    public Cell(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
public class Solution {

    public void solve(char[][] board) {
        if(board == null || board.length == 0 || board[0].length == 0) return;
        int m = board.length;
        int n = board[0].length;
        Queue<Cell> q = new LinkedList<>();
        int[][] directions = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        boolean [][] visited = new boolean[m][n];
        for (int j=0; j<board[0].length; j++) {
            if (board[0][j] == ‘O‘) q.offer(new Cell(0, j));
            if (board[board.length-1][j] == ‘O‘) q.offer(new Cell(board.length-1, j));
            visited[0][j] = true;
            visited[m - 1][j] =  true;
        }
        for (int i=0; i<board.length; i++) {
            if (board[i][0] == ‘O‘) q.offer(new Cell(i, 0));
            if (board[i][board[0].length-1] == ‘O‘) q.offer(new Cell(i, board[0].length-1));
            visited[i][0] = true;
            visited[i][n - 1] =  true;
        }
        while (!q.isEmpty()) {
            Cell cur = q.poll();
            board[cur.x][cur.y] = ‘$‘;

            for (int[] dir : directions) {
                int x = dir[0] + cur.x;
                int y = dir[1] + cur.y;
                if (x < 0 || y < 0 || x >= m || y >= n || visited[x][y] || board[x][y] == ‘X‘) {
                    continue;
                }
                visited[x][y] = true;
                q.offer(new Cell(x, y));
            }
        }
        for (int i=0; i<board.length; i++) {
            for (int j=0; j<board[0].length; j++) {
                if (board[i][j] == ‘X‘) continue;
                else if (board[i][j] == ‘$‘) board[i][j] = ‘O‘;
                else if (board[i][j] == ‘O‘) board[i][j] = ‘X‘;
            }
        }
    }
}

  

时间: 2025-01-02 00:48:52

130. Surrounded Regions的相关文章

leetcode 130 Surrounded Regions(BFS)

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

130. Surrounded Regions (Graph; DFS)

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

【一天一道LeetCode】#130. Surrounded Regions

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

130. 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

Java for LeetCode 130 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 XX O O XX X O XX O X X After running your function, the board should be:

[leedcode 130] 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

130. 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. For example, X X X X X O O X X X O X X O X X After running your function, th

[LeetCode]130 Surrounded Regions(DFS)

题目链接:https://leetcode.com/problems/surrounded-regions/?tab=Description 题意:把非边界的O改成X. 先dfs边界的,打好标记.把没变的变成X后同时更新标记的为O. 1 class Solution { 2 public: 3 const int dx[5] = {0, 0, 1, -1}; 4 const int dy[5] = {1, -1, 0, 0}; 5 int n, m; 6 bool ok(int x, int y

leetcode[130] Surrounded Regions

给定一个类似棋盘,有X和O,把X圈住的O变为X例如: 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,然后和四周的O相邻的O全都是不可能转化为X的,我们用另外一个符号标记,那么最后,矩阵中的O就是被圈住的,就需要转化为X,然后其他标记的符号就要转为原来的O.