Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 200-number-of-islands/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### step1

実装方法がわからなかったのでChatGPTを利用。
for文を回して'1'のマスに当たるたびに、countを増やしてdfsで隣接するマスをすべて'0'にすることを繰り返すだけだった。

### step2

変更点なし。

### step3

3回通すまで書き直し。
32 changes: 32 additions & 0 deletions 200-number-of-islands/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int m, n;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
5ky7/arai60#22 (comment)

また、メンバー変数の名前は、メンバー変数であることを明示するような命名にする場合があります。

https://google.github.io/styleguide/cppguide.html#Variable_Names

Data members of classes (but not structs) additionally have trailing underscores. For instance: a_local_variable, a_struct_data_member, a_class_data_member_.


void dfs(vector<vector<char>>& grid, int x, int y) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

dfs() という関数名は、現場のコードではあまり見かけないように思います。また関数名は動詞の原型・命令形から始めることが多いように思います。 traverse() などはいかがでしょうか?

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.

そうですね、traverseの方がdfsよりも意味のある関数名だと思います。

if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == '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.

範囲に関する条件式は、一直線上に並べるように書くと読みやすくなると思います。また、 1 つの条件式にあまり項を並べすぎないほうが読みやすくなると思います。

if (!(0 <= x && x < m && 0 <= y && y < n)) {
    return;
}

if (grid[x][y] == '0') {
    return;
}

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.

x/y の方向が縦/横となってしまっている点に違和感を感じました。 (r, c) (row, col) などを使うと、縦横の方向が一致し、違和感が無くなると思います。

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.

承知しました!

return;
}
grid[x][y] = '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.

こちらのコメントをご参照ください。
Hiroto-Iizuka/coding_practice#17 (comment)


dfs(grid, x + 1, y);
dfs(grid, x - 1, y);
dfs(grid, x, y + 1);
dfs(grid, x, y - 1);
}

int numIslands(vector<vector<char>>& grid) {
m = grid.size();
n = grid[0].size();
int count = 0;

for(int i = 0; i < m; i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

for 文のあとにスペースを空けることが多いと思います。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

if (b) { // Space after the keyword in conditions and loops.

for(int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
count++;
dfs(grid, i, j);
}
}
}
return count;
}
};
32 changes: 32 additions & 0 deletions 200-number-of-islands/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int m, n;

void dfs(vector<vector<char>>& grid, int x, int y) {
if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == '0') {
return;
}
grid[x][y] = '0';

dfs(grid, x + 1, y);
dfs(grid, x - 1, y);
dfs(grid, x, y + 1);
dfs(grid, x, y - 1);
}

int numIslands(vector<vector<char>>& grid) {
m = grid.size();
n = grid[0].size();
int count = 0;

for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
count++;
dfs(grid, i, j);
}
}
}
return count;
}
};
32 changes: 32 additions & 0 deletions 200-number-of-islands/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int m, n;

void dfs(vector<vector<char>>& grid, int x, int y) {
if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == '0') {
return;
}
grid[x][y] = '0';

dfs(grid, x + 1, y);
dfs(grid, x - 1, y);
dfs(grid, x, y + 1);
dfs(grid, x, y - 1);
}

int numIslands(vector<vector<char>>& grid) {
m = grid.size();
n = grid[0].size();
int count = 0;

for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
count++;
dfs(grid, i, j);
}
}
}
return count;
}
};