-
Notifications
You must be signed in to change notification settings - Fork 0
200. Number of Islands #17
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ### step1 | ||
|
|
||
| 実装方法がわからなかったのでChatGPTを利用。 | ||
| for文を回して'1'のマスに当たるたびに、countを増やしてdfsで隣接するマスをすべて'0'にすることを繰り返すだけだった。 | ||
|
|
||
| ### step2 | ||
|
|
||
| 変更点なし。 | ||
|
|
||
| ### step3 | ||
|
|
||
| 3回通すまで書き直し。 |
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dfs() という関数名は、現場のコードではあまり見かけないように思います。また関数名は動詞の原型・命令形から始めることが多いように思います。 traverse() などはいかがでしょうか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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;
}
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね、そちらの方が読みやすくなっていいと思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. x/y の方向が縦/横となってしまっている点に違和感を感じました。 (r, c) (row, col) などを使うと、縦横の方向が一致し、違和感が無くなると思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 承知しました! |
||
| return; | ||
| } | ||
| grid[x][y] = '0'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのコメントをご参照ください。 |
||
|
|
||
| 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++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for 文のあとにスペースを空けることが多いと思います。 https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
|
||
| for(int j = 0; j < n; j++) { | ||
| if (grid[i][j] == '1') { | ||
| count++; | ||
| dfs(grid, i, j); | ||
| } | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| }; | ||
| 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; | ||
| } | ||
| }; |
| 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; | ||
| } | ||
| }; |
There was a problem hiding this comment.
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