洪水填充慢速执行

时间:2019-02-10 06:54:23

标签: python-3.x flood-fill

输入:

arr = [["###########...."]
       ["#.......c.###.."]
       ["####......#.#.."]
       [".#.########.#.."]
       ["##...#..b#..#.."]
       ["#.a.#...#...###"]
       ["####.#.#d###..#"]
       ["......#...e#xx#"]
       [".#....#########"]
       [".#.x..#..#....."]
       [".######.c#....."]
       ["......####....."]]

请注意,#是单词{a, b, c, d等可以行进的范围,而.则表示有效的移动。我试图找出在一个单词居住的区域中是否还有另一个实体单词。

我认为洪水填充算法将是解决该问题的最佳选择,因为我可以在穿越该区域时检测到其他实体,所以我尝试这样做:

import collections

def patrol(grid, row, col, R, C, army, visited):
    danger = False
    visited.append([row, col]) 
    row_change = [-1, 0, 1, 0]
    col_change = [0, 1, 0, -1]

    for direction in range(4):

        new_row = row + row_change[direction]
        new_col = col + col_change[direction]
        if (new_row < 0) or (new_row >=  R) or (new_col < 0) or (new_col >= C) or (grid[new_row][new_col] == "#") :
            continue
        else:
            if ((grid[new_row][new_col] == ".") or (grid[new_row][new_col] == army)):
                if ([new_row, new_col] not in visited):
                    visited.append([new_row, new_col])
                    if(patrol(grid, new_row, new_col, R, C, army, visited)):
                        danger = True
                        return danger
                else:
                    continue
            else:
                danger = True
                return danger

    return danger

还有我的主要功能:

def main():
        #THIS IS THE INPUT
        really_raw_input = [2, 2, 2, ".#", "#a", 12, 15, '###########....', '#.......c.###..', '####......#.#..', '.#.########.#..', '##...#..b#..#..', '#.a.#...#...###', '####.#.#d###..#', '......#...e#xx#', '.#....#########', '.#.x..#..#.....', '.######.c#.....', '......####.....']
        number_of_game = int(really_raw_input.pop(0))

        results = []

        for _ in range(number_of_game):

            R = int(really_raw_input.pop(0))
            C = int(really_raw_input.pop(0))

            grid = []
            armies_coordinate = {}

            for row in range(R):
                grid.append(really_raw_input.pop(0))

                for character in set(grid[-1]):
                    if (character == "#") or (character == "."):
                        continue
                    else:
                        if character in armies_coordinate:
                            armies_coordinate[character].append([row, grid[-1].index(character)])
                        else:
                            armies_coordinate[character] = [[row, grid[-1].index(character)]]

            temp = armies_coordinate
            for army, coordinates in temp.items():
                    for coordinate in coordinates:
                        if (patrol(grid, coordinate[0], coordinate[1], R, C, army, [])):

    armies_coordinate[army].pop(armies_coordinate[army].index(coordinate))
    results.append(collections.OrderedDict(sorted(armies_coordinate.items())))

        return results

最后我跑了:

result = main()
contested = 0
iteration = 1

for coordinates in result:
    print("Case {}".format(iteration))
    for key, value in coordinates.items():
        if len(value) != 0:
            print("{} {}".format(key, len(value)))
        else:
            contested += 1

    print("Contested {}".format(int(contested/2)))

    iteration += 1

成功返回2个案例的结果。 但是,一旦我输入了更多的信息,比如说100个案例包含100行和列,则大约需要6分钟才能完成。对于我的要求来说已经很长了。

如何使它更快?

0 个答案:

没有答案