Leetcode: Walls and Gates

Question

You are given a m x n 2D grid initialized with these three possible values.

-1 - A wall or an obstacle.
0 - A gate.
INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.

For example, given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
Hide Company Tags Facebook
Hide Tags Breadth-first Search
Hide Similar Problems (M) Surrounded Regions (M) Number of Islands (H) Shortest Distance from All Buildings

My first try

if there are k zero elements,
size(rooms) = (m,n)
Time complexity: O( kmn )

class Solution(object):
    def wallsAndGates(self, rooms):
        """ :type rooms: List[List[int]] :rtype: void Do not return anything, modify rooms in-place instead. """

        if len(rooms)==0 or len(rooms[0])==0:
            return 

        zero_list = []
        for i in range(len(rooms)):
            for j in range(len(rooms[0])):
                if rooms[i][j]==0:
                    zero_list.append( (i,j) )


        for zero_item in zero_list:
            self.traverse4zero( zero_item, rooms )

    def traverse4zero(self, zero_item, rooms):
        #print "abc"
        bfs_list = [zero_item]
        dict_direct = [(-1,0), (1,0), (0,1), (0,-1)]
        visited = [ [0]*len(rooms[0]) for dummy in range(len(rooms)) ]
        visited[zero_item[0]][zero_item[1]] = 1

        while bfs_list!=[]:
            #print bfs_list
            cur_item = bfs_list[0]
            bfs_list = bfs_list[1:]
            self.eachdirection( rooms, cur_item, visited, bfs_list, dict_direct )

        return 


    def eachdirection(self, rooms, cur_item, visited, bfs_list, dict_direct):

        for i in range(4):
            next_i = cur_item[0] + dict_direct[i][0]
            next_j = cur_item[1] + dict_direct[i][1]
            if next_i<0 or next_i>=len(rooms) \
                or next_j<0 or next_j>=len(rooms[0]) \
                or rooms[next_i][next_j]==0 \
                or rooms[next_i][next_j]==-1 \
                or visited[next_i][next_j]==1 \
                or rooms[cur_item[0]][cur_item[1]]+1 >= rooms[next_i][next_j]:
                    continue

            rooms[next_i][next_j] = min( rooms[next_i][next_j], \
                        rooms[cur_item[0]][cur_item[1]]+1 )

            bfs_list.append( (next_i, next_j) )
            visited[next_i][next_j] = 1

        return       

Solution

Accept
Time: 540ms

class Solution(object):
    def wallsAndGates(self, rooms):
        """ :type rooms: List[List[int]] :rtype: void Do not return anything, modify rooms in-place instead. """

        if len(rooms)==0 or len(rooms[0])==0:
            return 

        zero_list = []
        for i in range(len(rooms)):
            for j in range(len(rooms[0])):
                if rooms[i][j]==0:
                    self.traverse4zero( (i,j), rooms )

    def traverse4zero(self, zero_item, rooms):
        bfs_list = [zero_item]
        dict_direct = [(-1,0), (1,0), (0,1), (0,-1)]

        while bfs_list!=[]:
            cur_item = bfs_list[0]
            bfs_list = bfs_list[1:]

            for i in range(4):
                next_i = cur_item[0] + dict_direct[i][0]
                next_j = cur_item[1] + dict_direct[i][1]
                if next_i<0 or next_i>=len(rooms) \
                    or next_j<0 or next_j>=len(rooms[0]) \
                    or rooms[next_i][next_j]<=0 \
                    or rooms[cur_item[0]][cur_item[1]]+1 >= rooms[next_i][next_j]:
                        continue

                rooms[next_i][next_j] = min( rooms[next_i][next_j], \
                            rooms[cur_item[0]][cur_item[1]]+1 )

                bfs_list.append( (next_i, next_j) )

        return

Solution2

Accept
Time: 588ms

This solution is different from solution1. BFS is done for each zero element at the same time.

class Solution(object):
    def wallsAndGates(self, rooms):
        """ :type rooms: List[List[int]] :rtype: void Do not return anything, modify rooms in-place instead. """

        if len(rooms)==0 or len(rooms[0])==0:
            return 

        bfs_list = []
        for i in range(len(rooms)):
            for j in range(len(rooms[0])):
                if rooms[i][j]==0:
                    bfs_list.append( (i,j) )

        dict_direct = [(-1,0), (1,0), (0,1), (0,-1)]
        while bfs_list!=[]:
            cur_item = bfs_list[0]
            del bfs_list[0] 

            for i in range(4):
                next_i = cur_item[0] + dict_direct[i][0]
                next_j = cur_item[1] + dict_direct[i][1]
                if next_i<0 or next_i>=len(rooms) \
                    or next_j<0 or next_j>=len(rooms[0]) \
                    or rooms[cur_item[0]][cur_item[1]]+1 >= rooms[next_i][next_j]:
                        continue

                rooms[next_i][next_j] = rooms[cur_item[0]][cur_item[1]]+1
                bfs_list.append( (next_i, next_j) )

        return       

Take home message

For popping the first element of list, use

del bfs_list[0]

instead of

bfs_list = bfs_list[1:]

你可能感兴趣的:(Leetcode: Walls and Gates)