link: https://leetcode.com/problems/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.
偷看了一下discussion仿照寫的,練習了并查集,基本思路是把所有的’O’連接起來,然後在boundary上的跟一個dummy node union,最後遍歷一遍找和dummy node不相連的所有的’O’;另外一開始我是用map來實現head/rk兩個數組,因為想讓index儲存坐標pair
my solution:
|
|
還可以直接floodfill搜索(相當於DFS),注意遞歸版本會爆棧,手動寫個stack就可以。
|
|
updated Dec 13:
BFS:
|
|