Max recursion depth exceeded - help?


  • 0
    A
    class Solution(object):
        def solve(self, board):
            """
            :type board: List[List[str]]
            :rtype: void Do not return anything, modify board in-place instead.
            """
            # Check outer loops if they have O!
            if board:    
                n = len(board[0])
                m = len(board)
                for i in range(m):
                    if board[i][0] == 'O':
                        self.dfs(board,i,0)
                    if board[i][n-1] == 'O':
                        self.dfs(board,i,n-1)
                        
                for j in range(n):
                    if board[0][j] == 'O':
                        self.dfs(board,0,j)
                    if board[m-1][j] == 'O':
                        self.dfs(board,m-1,j)
                for i in range(m):
                    for j in range(n):
                        if board[i][j] == 'O':
                            board[i][j] = 'X'
                        elif board[i][j] == 'A':
                            board[i][j] = 'O'
                            
                
        def dfs(self,board,i,j):
            # Base case~break?
            m = len(board[0])
            n = len(board)
            if (i<0) or (i>n-1) or (j<0) or (j>m-1):
                return #nothing?
            if board[i][j] == 'O': 
                board[i][j] = 'A'
            if i>0 and board[i-1][j] == 'O' : self.dfs(board,i-1,j)
            if i<n-1 and board[i+1][j] == 'O' :self.dfs(board,i+1,j)
            if j>0 and board[i][j-1] == 'O' :self.dfs(board,i,j-1)
            if j<m-1 and board[i][j+1] == 'O' :self.dfs(board,i,j+1)
            
    

    I can't quite figure out why it's exceeding max recursion depth
    @StefanPochmann can you help? i've been reading and learning from your answers a lot!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.