I was solving a collection of paiza level-up questions, but I didn't have a model answer, so I made it myself. The language is Python3.
Paiza's skill check sample problem "Island search (equivalent to paiza rank S)" https://paiza.jp/works/mondai/skillcheck_sample/search-island?language_uid=python3 I couldn't see the problem statement without logging in. Registration is free and can be done immediately, so I recommend you to register for the time being.
find_lands.py
col, row = map(int, input().split())
map_list = [[0]* (col+2)]
for _ in range(row):
map_list.append([0] + list(map(int, input().split())) + [0])
map_list.append(map_list[0])
def check(x, y):
lands = [[x,y]]
while lands:
x, y = lands.pop()
map_list[y][x] = 0
# down
if map_list[y+1][x] == 1:
lands.append([x, y+1])
# right
if map_list[y][x+1] == 1:
lands.append([x+1, y])
# up
if map_list[y-1][x] == 1:
lands.append([x, y-1])
# left
if map_list[y][x-1] == 1:
lands.append([x-1, y])
count = 0
for r in range(1, row+1):
for c in range(1, col+1):
if map_list[r][c] == 1:
check(c, r)
count += 1
print(count)
https://maro28.com/paiza-s-rank-mod7
I wrote an article about Qiita at the beginning, but it should be simple and easy to use! I hope I can use it from now on.
Recommended Posts