Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
depth-first search solution in Clear category for Eaten Go Stones by David_Jones
from itertools import product
def neighbors(cell):
(x,y) = cell
yield from ((x-1,y), (x+1,y), (x,y-1), (x,y+1))
def go_game(board):
def count_eaten(stones):
eaten = 0
while stones:
group, alive = set(), False
stack = [stones.pop()]
while stack:
cell = stack.pop()
group.add(cell)
for neighbor in neighbors(cell):
if neighbor in stones:
stack.append(neighbor)
stones.remove(neighbor)
elif neighbor in empty:
alive = True
if not alive:
eaten += len(group)
return eaten
black, empty, white = set(), set(), set()
for (x,y) in product(range(len(board)), repeat=2):
if board[x][y] == 'B':
black.add((x,y))
elif board[x][y] == 'W':
white.add((x,y))
else:
empty.add((x,y))
return {'B': count_eaten(black), 'W': count_eaten(white)}
June 30, 2019