Gamified challenge · DFS from the border
board is a map where each string is a row of X (your walls) and O (defenders). Defenders that are next to each other up, down, left or right form one group. A group is captured when none of its cells is on the edge of the map: it has no way out.
Write capture(board) that returns the board after the siege: every O in a captured group becomes X; everything else stays the same.
Example: ["XXXX", "XOOX", "XXOX", "XOXX"] → ["XXXX", "XXXX", "XXXX", "XOXX"]. The middle group is sealed in, but the O in the bottom row is on the edge and escapes.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google