Files
@ 07f3fda1cbd7
Branch filter:
Location: OneEye/src/go/helperboard.py - annotation
07f3fda1cbd7
1.2 KiB
text/x-python
optimized go floodFill
07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 07f3fda1cbd7 | from util import EMPTY
## Utility class for finding continuous regions on a Go.board.
class HelperBoard:
def __init__(self,board):
self._refBoard=board # Go.board, readonly
self._boardSize=len(board)
self._board=[[EMPTY]*self._boardSize for i in range(self._boardSize)]
self._usedCoords=[0]*(self._boardSize**2)*2
self._usedCount=0
def floodFill(self,filling,r,c,needle=None):
if c<0 or c>=self._boardSize or r<0 or r>=self._boardSize: return False # out of range
if self._board[r][c]: return False # already visited
if self._refBoard[r][c]==needle: return True # found something
if self._refBoard[r][c]!=filling: return False # out of area boundary
self._board[r][c]=True # set visited
self._usedCoords[self._usedCount*2]=r
self._usedCoords[self._usedCount*2+1]=c
self._usedCount+=1
# check neighbors
return self.floodFill(filling,r,c-1) or \
self.floodFill(filling,r,c+1) or \
self.floodFill(filling,r-1,c) or \
self.floodFill(filling,r+1,c)
def continuousArea(self):
for i in range(self._usedCount):
yield (self._usedCoords[i*2],self._usedCoords[i*2+1])
def clear(self):
for i in range(self._usedCount):
(r,c)=(self._usedCoords[i*2],self._usedCoords[i*2+1])
self._board[r][c]=EMPTY
self._usedCount=0
|