Skip to content

number-of-islands#17

Open
n6o wants to merge 1 commit intomainfrom
number-of-islands
Open

number-of-islands#17
n6o wants to merge 1 commit intomainfrom
number-of-islands

Conversation

@n6o
Copy link
Copy Markdown
Owner

@n6o n6o commented Feb 14, 2026

今回の問題

Number of Islands - LeetCode

使用言語

Go

次に解く問題

Max Area of Island - LeetCode


### BFSで島を数える

- grid のコピーを用意する
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これってコピーの必要があるんですか?
直感的に来訪済みを1から0に変えているのであればin-placeでできそうだと思いました。

Go言語触ったことないので言語的な仕様が関係していたらすみません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

この練習会の中では、私は呼び出し元から渡されたものを破壊しないようにする作法としてコピーして使うようにしています。(コピーすることでメモリ等に大きな影響がある状況などではしないこともありえます)

記載いただいたように、そのまま利用することも可能です。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そういう意図だったんですね。ありがとうございます。勉強になります

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

まあ、呼び出し側の気持ちとなりましょう。
お仕事をしていて、
「地図の自動生成機能を作ったぞ。あ、でも、島の数が多すぎるときは警告メッセージを出したいな。中の島の数を数える機能が必要だけど、時間がない。あ、そうだ。最近入ってきたあの人にちょっとお願いしよう。」
「できました!」
「ありがとう。」

grid := generateMap()
count := numIslands(grid)
if count > 100 {
   ...
}

grid は破壊されました。

copyOfGrid := copyGrid(grid)

var count int
for i := range copyOfGrid {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

既に検討されたことと思いますが、Pointを含め全体を通して i, j を row, col にした方が座標(?)をイメージしやすいと思います。
個人的な体験の話になりますが、高校を卒業してからかなり長い間、二次元配列を扱う時に x, y ないし i, j を使用していました。これは中学・高校数学で長く扱った典型的なXY座標系のイメージが払拭できずにいたからなのですが、その後行列、その具体例としてエクセルやpandas dataframeに触れ、そちらのイメージを採用したところ、やっと周りの人との感覚と合致したように思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
私自身も混乱することが多いので、grid を扱う場合は row / column を採用するようにします。

j: p.j + offset.j,
}

if adjacent.i < 0 || adjacent.j < 0 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この切り分け方も理解できますが、

            if !(0 <= adjacent.row && adjacent.row < len(grid)) {
                continue
            }
            if !(0 <= adjacent.col && adjacent.col < len(grid[0])) {
                continue
            }

rowが範囲内になかったら -> continue
columnが範囲内になかったら-> continue

とするといい感じに条件が脳内で抽象しやすいように思います。if adjacent.i < 0 || adjacent.j < 0 {は、なんとなく登場人物が二人でてきているようなイメージがあります。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

提案いただいた表現のほうが row / column それぞれの条件チェックという意図が伝わる気がしました。
以降の指針にいたします。

continue
}

if grid[x][y] == '1' {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

x と y の縦横の意味が逆転しているように思いました。 (r, c) (row, col) (row, column) といったへんすうにすると、順番と意味が一致すると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

ご指摘の通り、記載する場所と意味をよく考えずに字面でそれっぽい感じに書いていました。
グリットを扱う場合は row / column を使うようにします。

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.

5 participants