|
| 1 | +public class jinvicky { |
| 2 | +} |
| 3 | + |
| 4 | +/** |
| 5 | + * 일관성을 위해 모든 변수들을 메서드 내 lv로 선언해서 푸는 것으로 한다. |
| 6 | + * dfs를 설계할 때 특히 반환 타입에 약한데 void인가 int,booelan 등 기타 타입인가를 확실히 하기 위해 별도 예제로 풀었다. |
| 7 | + * 또한 의도적으로 기존 grid를 1 -> 0으로 바꾸어 섬을 없애지 않고 공간복잡도가 증가하더라도 학습을 위해 방문 배열을 별도로 선언해 방문 여부를 체크했다. |
| 8 | + */ |
| 9 | +class Solution { |
| 10 | + public int numIslands1(char[][] grid) { |
| 11 | + if (grid == null || grid.length == 0) return 0; |
| 12 | + int rows = grid.length, cols = grid[0].length; |
| 13 | + boolean[][] visited = new boolean[rows][cols]; |
| 14 | + |
| 15 | + int count = 0; |
| 16 | + for (int r = 0; r < rows; r++) { |
| 17 | + for (int c = 0; c < cols; c++) { |
| 18 | + if (grid[r][c] == '1' && !visited[r][c]) { |
| 19 | + int size = dfsByInt(grid, visited, r, c, rows, cols); |
| 20 | + if (size > 0) count++; // 크기가 양수면 섬 1개 |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + return count; |
| 25 | + } |
| 26 | + |
| 27 | + // dfs는 해당 섬의 넓이를 반환 |
| 28 | + private int dfsByInt(char[][] grid, boolean[][] visited, int r, int c, int rows, int cols) { |
| 29 | + if (r < 0 || c < 0 || r >= rows || c >= cols) return 0; // 범위를 벗어났다. |
| 30 | + if (grid[r][c] == '0' || visited[r][c]) return 0; // 문제의 조건에 맞지 않거나, 이미 방문했다. |
| 31 | + |
| 32 | + visited[r][c] = true; // 방문 처리 |
| 33 | + int area = 1; |
| 34 | + |
| 35 | + area += dfsByInt(grid, visited, r + 1, c, rows, cols); |
| 36 | + area += dfsByInt(grid, visited, r - 1, c, rows, cols); |
| 37 | + area += dfsByInt(grid, visited, r, c + 1, rows, cols); |
| 38 | + area += dfsByInt(grid, visited, r, c - 1, rows, cols); |
| 39 | + |
| 40 | + return area; |
| 41 | + } |
| 42 | + |
| 43 | + public int numIslands(char[][] grid) { |
| 44 | + if (grid == null || grid.length == 0) return 0; |
| 45 | + int rows = grid.length, cols = grid[0].length; |
| 46 | + |
| 47 | + boolean[][] visited = new boolean[rows][cols]; |
| 48 | + int count = 0; |
| 49 | + |
| 50 | + for (int r = 0; r < rows; r++) { |
| 51 | + for (int c = 0; c < cols; c++) { |
| 52 | + if (grid[r][c] == '1' && !visited[r][c]) { |
| 53 | + dfs(grid, visited, r, c, rows, cols); |
| 54 | + count++; // 섬 하나 탐색 끝나면 +1 |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return count; |
| 59 | + } |
| 60 | + |
| 61 | + private void dfs(char[][] grid, boolean[][] visited, int r, int c, int rows, int cols) { |
| 62 | + if (r < 0 || c < 0 || r >= rows || c >= cols) return; // 그리드 범위를 벗어나면 종료 |
| 63 | + if (grid[r][c] == '0' || visited[r][c]) return; // 물이거나 이미 방문한 섬이라면 종료 |
| 64 | + |
| 65 | + visited[r][c] = true; // 방문 처리 |
| 66 | + |
| 67 | + // 상하좌우 DFS |
| 68 | + dfs(grid, visited, r + 1, c, rows, cols); |
| 69 | + dfs(grid, visited, r - 1, c, rows, cols); |
| 70 | + dfs(grid, visited, r, c + 1, rows, cols); |
| 71 | + dfs(grid, visited, r, c - 1, rows, cols); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * void 반환의 dfs면 return; |
| 76 | + * int 반환의 dfs면 return 0; |
| 77 | + * bool 반환의 dfs면 return false; |
| 78 | + * |
| 79 | + * 1. 리턴값 없이 넘겨받은 상태만 갱신 |
| 80 | + * void dfs(Node node) { |
| 81 | + * if (범위 밖 || 조건 불만족 || 이미 방문) return; |
| 82 | + * |
| 83 | + * 방문 처리(node); |
| 84 | + * |
| 85 | + * for (이웃 nei : node) { |
| 86 | + * dfs(nei); |
| 87 | + * } |
| 88 | + * } |
| 89 | + * |
| 90 | + * 2. 최대/최소 값을 정수로 반환 |
| 91 | + * int dfs(Node node) { |
| 92 | + * if (범위 밖 || 조건 불만족) return 0; |
| 93 | + * |
| 94 | + * 방문 처리(node); |
| 95 | + * |
| 96 | + * int result = 1; // 자기 자신 포함 |
| 97 | + * for (이웃 nei : node) { |
| 98 | + * result += dfs(nei); |
| 99 | + * } |
| 100 | + * return result; |
| 101 | + * } |
| 102 | + * 3. 조건 만족 여부 |
| 103 | + * boolean dfs(Node node) { |
| 104 | + * if (목표 도달) return true; |
| 105 | + * if (범위 밖 || 조건 불만족) return false; |
| 106 | + * |
| 107 | + * 방문 처리(node); |
| 108 | + * |
| 109 | + * for (이웃 nei : node) { |
| 110 | + * if (dfs(nei)) return true; |
| 111 | + * } |
| 112 | + * return false; |
| 113 | + * } |
| 114 | + * |
| 115 | + * 4. 메모이제이션/DP 결합 |
| 116 | + * int dfs(Node node, Map<Node,Integer> memo) { |
| 117 | + * if (memo.containsKey(node)) return memo.get(node); |
| 118 | + * if (기저 조건) return 1; |
| 119 | + * |
| 120 | + * int best = 0; |
| 121 | + * for (이웃 nei : node) { |
| 122 | + * best = Math.max(best, 1 + dfs(nei, memo)); |
| 123 | + * } |
| 124 | + * memo.put(node, best); |
| 125 | + * return best; |
| 126 | + * } |
| 127 | + */ |
| 128 | +} |
0 commit comments