We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b23b0dd commit c05a693Copy full SHA for c05a693
1 file changed
허현빈/4주차/260121.js
@@ -0,0 +1,41 @@
1
+// 오늘 너무 피곤하다.. 날먹 dfs...rrr
2
+
3
+var numIslands = function (grid) {
4
+ const dir = [
5
+ [1, 0],
6
+ [-1, 0],
7
+ [0, 1],
8
+ [0, -1],
9
+ ];
10
11
+ const bfs = (y, x) => {
12
+ const q = [[y, x]];
13
+ while (q.length) {
14
+ const [curRow, curCol] = q.shift();
15
+ for (let i = 0; i < dir.length; i++) {
16
+ const ny = curRow + dir[i][0];
17
+ const nx = curCol + dir[i][1];
18
+ if (
19
+ nx >= 0 &&
20
+ nx < grid[0].length &&
21
+ ny >= 0 &&
22
+ ny < grid.length &&
23
+ grid[ny][nx] === "1"
24
+ ) {
25
+ grid[ny][nx] = "0";
26
+ q.push([ny, nx]);
27
+ }
28
29
30
+ };
31
+ let count = 0;
32
+ for (let i = 0; i < grid.length; i++) {
33
+ for (let j = 0; j < grid[0].length; j++) {
34
+ if (grid[i][j] === "1") {
35
+ bfs(i, j);
36
+ count++;
37
38
39
40
+ return count;
41
+};
0 commit comments