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.
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.
給予一個二維面板包含
'X'
和'O'
(字母 O),佔領所有被'X'
包圍的地區。
反轉所有
'O'
使其成為'X'
來佔領一個區域。
解釋:
被佔領的區域不應該在邊界上,因此任何在邊界上的'O'
都不應該被反轉為'X'
。任何'O'
不在邊界上或是沒有和邊界連接到的都會被翻轉成'X'
。兩個格子連接是指他們水平或垂直連接。
'O'
。O
轉換為暫時符號'N'
,接著DFS遍歷所有邊界上的'N'
,將其轉回'O'
。最後將所有沒有被遍歷到的暫時符號'N'
轉成'X'
。LeetCode
C++