BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Reinforcement learning24 of 36 in topic

605. Find a Go group and count its liberties with flood fill

MediumReinforcement learning Pro

Why it matters Liberty counting decides captures, the core rule every Go engine and self-play RL environment must get right.

Go engines and self-play agents constantly ask: which stones are connected to this one, and how many empty points touch the group?

Write group_liberties(board, r, c). board is a list of equal-length strings using "." for empty, "B" for black, "W" for white. Stones connect only up, down, left and right. If (r, c) is empty, return ([], 0). Otherwise flood fill the group of the same color containing (r, c) and return (sorted list of (row, col) tuples in the group, number of DISTINCT empty points adjacent to any stone of the group).

Functiondef group_liberties(board, r, c)

Example 1

Input: board ["BB.", "W..", "..."], r 0, c 0

Output: ([(0, 0), (0, 1)], 2)

Example 2

Input: board = ['.B.', 'B.B', '.B.'], r = 0, c = 1

Output: ([(0, 1)], 3)

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Use a stack or queue; collect liberties in a set so shared points count once.

Hint 2

Only 4-neighbors connect, never diagonals.

Hint 3

Keep a visited set for stones and a separate set for liberties.

Hint 4

Sort the group before returning.

Common mistakes
  • Counting a shared liberty more than once.
  • Connecting stones diagonally.
  • Running off the board edge.
Understand the idea

In Go, stones of one color that touch up, down, left or right form a group that lives or dies together. The empty points next to a group are its liberties, its breathing room. When a group has zero liberties it is captured.

Worked example

board rows "BB.", "W..", "...". The black stones at (0, 0) and (0, 1) are connected. (0, 0) touches the white stone below it, so no liberty there. (0, 1) touches (0, 2) and (1, 1), both empty. The group has 2 liberties.

flood fill from the start stone over same-colored 4-neighbors; liberties = set of empty 4-neighbors of all group stones; count the set

Every Go environment used for self-play RL, including the ones that trained AlphaGo-style agents, runs this flood fill after every move to find captures and illegal suicides.

Topics
Reinforcement learning
Similar questions
  • Run tabular Q-learning updatesMedium
  • Pick the next move in MCTS with the PUCT ruleMedium
  • Compute a REINFORCE policy-gradient update with a baselineMedium
  • Back up a playout value through an MCTS path with alternating playersMedium
All Reinforcement learning problems
Report
605 / 903
Python 3solution.pyPython idle
1
2
3
4
def group_liberties(board, r, c):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS