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 be:

X X X X
X X X X
X X X X
X O X X
class Solution {
    struct position
    {
        int x, y;
        position(int a, int b): x(a), y(b) {}
    };
public:
    void solve(vector<vector<char>>& board) {
        int row = board.size();
        if(row <= 0)
            return;
        int col = board[0].size();
        if(col <= 0)
            return;
        queue<position> q;
        int i, j;
        for(i = 0; i < col; i++)
        {
            if(‘O‘ == board[0][i])
                q.push(position(0, i));
            if(‘O‘ == board[row-1][i])
                q.push(position(row-1, i));
        }
        for(i = 0; i < row; i++)
        {
            if(‘O‘ == board[i][0])
                q.push(position(i, 0));
            if(‘O‘ == board[i][col-1])
                q.push(position(i, col-1));
        }
        while(!q.empty())
        {
            position p = q.front();
            q.pop();
            board[p.x][p.y] = ‘N‘;
            if(p.x > 0 && ‘O‘ == board[(p.x)-1][p.y])
                q.push(position((p.x)-1, p.y));
            if(p.x < row-1 && ‘O‘ == board[(p.x)+1][p.y])
                q.push(position((p.x)+1, p.y));
            if(p.y > 0 && ‘O‘ == board[p.x][(p.y)-1])
                q.push(position(p.x, (p.y)-1));
            if(p.y < col-1 && ‘O‘ == board[p.x][(p.y)+1])
                q.push(position(p.x, (p.y)+1));
        }
        for(i = 0; i < row; i++)
        {
            for(j = 0; j < col; j++)
            {
                if(‘O‘ == board[i][j])
                    board[i][j] = ‘X‘;
                if(‘N‘ == board[i][j])
                    board[i][j] = ‘O‘;
            }
            cout<<endl;
        }
    }
};

先找到四条边缘上面的字符,再找和这些字符相邻的字符。

时间: 2024-10-07 17:36:02

130. Surrounded Regions -- 被某字符包围的区域的相关文章

【一天一道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

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

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