Skip to content

200. Number of Islands #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

200. Number of Islands #16

wants to merge 2 commits into from

Conversation

rinost081
Copy link
Owner

No description provided.



- https://github.com/olsen-blue/Arai60/pull/17/files
この方の思考ログで"これはvisitedでグリッド管理するのではなく、陸地を見つけたら島1つ丸ごと消し去って海にしていくイメージが好き。(逆に、池の数を求める場合は、池を埋め立てて陸にする。)"と書かれていて, 確かに面白い発想だなと思った一方で, 小田さんが["入力を破壊していいかどうかは状況次第"](https://github.com/olsen-blue/Arai60/pull/17/files#r1915967908)とおっしゃっていてそちらにも納得した. 小田さんのコメントを意識した上で, この方のような発想を持てたら良いと感じた.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

プログラムを書くというのは、「それを呼び出す人のために何かをしてあげる」ということなのです。(エンジニアかもしれないし、ウェブの向こうのユーザーかもしれません。)
なので、そこまで遠くまで考えてみましょうということかと思います。

num_columns = len(grid[0])

def inside_island(row, column):
return 0 <= row < num_rows and 0 <= column < num_columns
Copy link

@potrue potrue May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちら、たぶん"island(島)"というと連続したlandからなるいくつかのマス目の集合、というイメージになるので、単にgridの範囲内にあるかどうかだけであればis_inside_gridとかにしてしまった方がよいと思います。

また、この関数にgrid[row][column] != WATERおよび(row, column) not in visited_islandの条件もまとめてしまって、下の二つのfor文で使ってもよいと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにこうしてみると、island, landそれと全体的なマップ(?)がごちゃごちゃしていますね。is_inside_grid良い気がしてきました

def traverse_island(i, j):
if (i, j) in visited_island:
return
if i < 0 or num_rows <= i or j < 0 or num_columns <= j:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

step3 で書いている条件の否定の書き方が個人的に好きです。
not (0 <= row < num_rows and 0 <= column < num_columns)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

引用しておきます。
https://docs.python.org/3/reference/expressions.html#comparisons

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

num_islands += 1

return num_islands
```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inside_island について potrue さんと同じ意見です。
細かいですが、island = deque([(row, column)]) は island を構成する陸が格納されているのでわかりますが、visited_island は visited_lands の方が適切な感じを受けました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

islandとlandについて、あまり深く考えていませんでしたがその通りですね

for (i, j) in ((1, 0), (-1, 0), (0, 1), (0, -1)):
if not inside_island(r + i, c + j):
continue
elif grid[r + i][c + j] == WATER:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この辺の elif は私は if で書きます。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

書きながら思っていたのですが、まあいいかとelifを使っていました。

elifを使う場合はそれが条件文として対応している場合に限定した方が良いですね

return 0 <= i < num_rows and 0 <= j < num_columns

def traverse_island(i, j):
island = deque([(i, j)])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

island は land の集合を表すため、不適切であるように感じます。 lands としたらどうかと思いましたが、 land は不加算名詞のため、 s を付けるのは不適切のようです。
自分はよく frontier という言葉を使います。いかがでしょうか?

return 0 <= row < num_rows and 0 <= column < num_columns

def traverse_island(row, column):
island = deque([(row, column)])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

traverse_island 呼び出し時の row と column は visited_island に追加されないまま探索が始まるので、これだけ二回キューに入れられるのかなと思いました。個人的にはここでも visited_island に追加しておきたい気持ちがありました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants