Python BFS solution


#1

This problem can be solved using BFS as well,although not very efficient than the actual solution.

class Solution:
    # @param A : integer
    # @param B : integer
    # @param C : integer
    # @param D : integer
    # @return an integer
    def solve(self, A, B, C, D):
        q = []
        visited=set()
        count = 0
        q.append((A,B))
        visited.add((A,B))
        while q:
            x,y = q.pop()
            if x!=A and y+1!=B and x<C and y+1<D and (x,y+1) not in visited:
                q.append((x,y+1))
                visited.add((x,y+1))
                count+=1
            if x+1!=A and y!=B and x+1<C and y<D and (x+1,y) not in visited:
                q.append((x+1,y))
                visited.add((x+1,y))
                count+=1
            if x+1!=A and y+1!=B and x+1<C and y+1<D and (x+1,y+1) not in visited:
                q.append((x+1,y+1))
                visited.add((x+1,y+1))
                count+=1
        return count
        

        # logical solution below of just one line
        return (D-B-1)*(C-A-1)

#2

Thank you soo much man! It helped me.