Created
September 9, 2022 21:10
-
-
Save shiracamus/67345e5f8f5e989c8a75bab55d51359c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(garden): | |
days = 0 | |
# 咲いている花(1)の周囲で咲いていない花(0)、つまり次に咲く花の位置を列挙 | |
grows = {(ny, nx) | |
for y, row in enumerate(garden) | |
for x, flower in enumerate(row) | |
if flower == 1 | |
for ny, nx in ((y-1, x), (y+1, x), (y, x-1), (y, x+1)) | |
if 0 <= ny < len(garden) and 0 <= nx < len(garden[ny]) | |
if garden[ny][nx] == 0} | |
# 次に咲く花がある間 | |
while grows: | |
days += 1 | |
# 花を咲かせる | |
for y, x in grows: | |
garden[y][x] = 1 | |
# 新たに咲いた花の周囲で次に咲く花の位置を列挙する | |
grows = {(ny, nx) | |
for y, x in grows | |
for ny, nx in ((y-1, x), (y+1, x), (y, x-1), (y, x+1)) | |
if 0 <= ny < len(garden) and 0 <= nx < len(garden[ny]) | |
if garden[ny][nx] == 0} | |
return days | |
n = 600 | |
garden = [[0 for i in range(n)] for j in range(n)] | |
garden[0][0] = 1 | |
garden[5][5] = 1 | |
garden[n-1][n-5] = 1 | |
print(solution(garden)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment