LoginSignup
0
3

More than 3 years have passed since last update.

Pythonで島探し (paizaランク S 相当) を解く

Last updated at Posted at 2020-02-26

初めに

paizaのレベルアップ問題集を解いていたのですが、模範解答がなかったので自分で作ってみました。
言語はPython3です。

問題

Paizaのスキルチェック見本問題「島探し (paizaランク S 相当)」
https://paiza.jp/works/mondai/skillcheck_sample/search-island?language_uid=python3
ログインしないと問題文が見れませんでした。
登録は無料ですぐにできるので、とりあえず登録してみることをおススメします。

解答コード

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)

参考

まとめ

初めでQiitaの記事を書いてみましたが、シンプルで使いやすくていいですね!
これからちょくちょく使っていけたらいいなと思います。

0
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3