Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
20-liner: by stack solution in Clear category for The Territory of Go by przemyslaw.daniel
def floodfill(x, y, board):
stack, black, white, size = [(x, y)], False, False, 0
while stack:
x, y = stack.pop()
if not (0 <= x < len(board) and 0 <= y < len(board)):
continue
black |= board[x][y] == 'B'
white |= board[x][y] == 'W'
if board[x][y] != '+': continue
board[x][y], size = 'x', size + 1
for i, j in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
stack += [(i, j)]
return size*(black+white*1j)*(black != white)
def territory(board):
board = [list(x) for x in board]
result, size = 0, len(board)
for i, j in (divmod(x, size) for x in range(size**2)):
result += floodfill(i, j, board)
return {'B': int(result.real), 'W': int(result.imag)}
June 13, 2018
Comments: