Blue Bridge Cup diffusion python implementation

Article Directory

I'm a good cook, this question is still a solution to the problem, hey. Why do you know how to solve the problem?

problem

Xiaolan paints on an infinitely large special canvas. This canvas can be seen as a grid graph, and each grid can be represented by a two-dimensional integer coordinate.
Xiao Lan first clicked a few points on the canvas: (0, 0), (2020, 11), (11, 14), (2000, 2000).
Only these grids are black, and the other positions are white. Every minute, the black will spread a little. Specifically, if a grid is black, it
will spread to the four adjacent grids on the top, bottom, left and right, making these four grids also black (if it was originally black, it will still be black). Excuse me, after 2020 minutes, how many squares on the canvas are black.

Idea and code

Include all the points in the map. If you walk within 2020 steps, then you can dye. Note that you cannot repeat dyeing, because there are four paths to the same point (four starting points are given in this question)

lis = [[0,0],[2020,11],[11,14],[2000,2000]]
n = 0
for x in range(-2020,2000+2020+1):
    for y in range(-2020,2000+2020+1):
        for i in range(4):
            if abs(x-lis[i][0])+abs(y-lis[i][1]) <= 2020:
                n+=1
                break  # 防止重复染色
print(n)
                

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_49821869/article/details/115003001