-
[LeetCode] Surrounded Regions알고리즘 2019. 12. 7. 04:12
문제
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.
Approach
BFS로 푼다. 벡터에 X로 바꿀 O의 좌표를 저장하되, 가장자리가 뚫려있으면 바꾸지 않는다.
Code
https://github.com/chi3236/algorithm/blob/master/LeetCode_SurroundedRegions.cpp
'알고리즘' 카테고리의 다른 글
[LeetCode] Gas Station (0) 2019.12.13 [LeetCode] Palindrome Partitioning (0) 2019.12.07 [LeetCode] Longest Consecutive Sequence (0) 2019.12.06 [LeetCode] Single Number (0) 2019.12.06 [LeetCode] Word Ladder (0) 2019.12.06