LeetCode | 130. Surrounded Regions

3001 ワード


タイトル:
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.
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

Explanation:
Surrounded regions shouldn’t be on the border, which means that any  'O'  on the border of the board are not flipped to  'X' . Any  'O'  that is not on the border and it is not connected to an  'O'  on the border will be flipped to  'X' . Two cells are connected if they are adjacent cells connected horizontally or vertically.
 
コード:
class Solution {
public:
    void solve(vector>& board) {
        if(board.size() < 1)
            return;
        int h = board.size();
        int w = board[0].size();
        if(w < 1)
            return;
        
        vector> record(h, vector(w, 0));
        vector> mboard(h, vector(w, 0));
        
        queue> q;
        for(int i = 0; i(i, j));
                        mboard[i][j] = 1;
                    }
                    record[i][j] = 1;
                }
            }
        }
        
        vector> d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        while(!q.empty())
        {
            pair cur = q.front();
            q.pop();
            pair pos;
            for(int k = 0; k < 4; k++)
            {
                pos.first = cur.first + d[k][0];
                pos.second = cur.second + d[k][1];
                if(pos.first < 0 || pos.first >= h || pos.second < 0 || pos.second >= w)
                    continue;
                
                if(board[pos.first][pos.second] == 'O' && !record[pos.first][pos.second])
                {
                    mboard[pos.first][pos.second] = 1;
                    q.push(pos);
                }
                
                record[pos.first][pos.second] = 1;
            }
        }
        
        for(int i = 0; i

一回AC、効率はあまり高くないけど~
 
余談:
あっという間に7月末になった.七月は、一年の中で一番好きな月ですが、今回は急いで行きました.
もしかすると、仕事を続ける疲れがあり、複数の項目が交差して重なる暇がなく、時間管理の怠慢があるかもしれません.
しかし、楽しいこともあり、焦りながらも着実に進んでおり、テイラーはfolkloreをリリースし、静かに統一されたアルバムで、聴き応えがあり癒されます.
今週末は完全にリラックスしてほしいですね.そして次のことをしっかり計画して~適当に引き算をして、注意力の利用率を高めます.
Anyway, hope for the best.
七月、さようなら.
こんにちは、八月です.