|
| 1 | +// DFS. Accepted. Time and Space: O(mn) |
| 2 | +class Solution { |
| 3 | + func closedIsland(_ grid: [[Int]]) -> Int { |
| 4 | + guard grid.count > 2, |
| 5 | + grid[0].count > 2 else { |
| 6 | + return 0 |
| 7 | + } |
| 8 | + var (grid, ilandCount) = (grid, 0) |
| 9 | + for row in 1..<grid.count - 1 { |
| 10 | + for col in 1..<grid[0].count - 1 { |
| 11 | + let value = grid[row][col] |
| 12 | + if value == 0 { |
| 13 | + grid[row][col] = 2 |
| 14 | + ilandCount += dfs(&grid, row, col) |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + return ilandCount |
| 19 | + } |
| 20 | + |
| 21 | + @discardableResult |
| 22 | + func dfs(_ grid: inout [[Int]], _ row: Int, _ col: Int) -> Int { |
| 23 | + var results = [Int]() |
| 24 | + for (newRow, newCol) in [(row - 1, col), (row, col + 1), (row + 1, col), (row, col - 1)] { |
| 25 | + if newRow >= 0 && newRow < grid.count && newCol >= 0 && newCol < grid[0].count { |
| 26 | + if newRow == 0 || newRow == grid.count - 1 || newCol == 0 || newCol == grid[0].count - 1 { |
| 27 | + if grid[newRow][newCol] == 0 { |
| 28 | + results.append(0) |
| 29 | + grid[newRow][newCol] = 2 |
| 30 | + dfs(&grid, newRow, newCol) |
| 31 | + } |
| 32 | + } else { |
| 33 | + if grid[newRow][newCol] == 0 { |
| 34 | + grid[newRow][newCol] = 2 |
| 35 | + results.append(dfs(&grid, newRow, newCol)) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + return results.allSatisfy { $0 == 1 } ? 1 : 0 |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// BFS. Accepted. Time and Space: O(mn) |
| 45 | +class Solution { |
| 46 | + func closedIsland(_ grid: [[Int]]) -> Int { |
| 47 | + guard grid.count > 2, |
| 48 | + grid[0].count > 2 else { |
| 49 | + return 0 |
| 50 | + } |
| 51 | + var (grid, ilandCount) = (grid, 0) |
| 52 | + for row in 1..<grid.count - 1 { |
| 53 | + for col in 1..<grid[0].count - 1 { |
| 54 | + let value = grid[row][col] |
| 55 | + if value == 0 { |
| 56 | + grid[row][col] = 2 |
| 57 | + ilandCount += bfs(&grid, row, col) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return ilandCount |
| 62 | + } |
| 63 | + |
| 64 | + @discardableResult |
| 65 | + func bfs(_ grid: inout [[Int]], _ row: Int, _ col: Int) -> Int { |
| 66 | + var isClosed = true |
| 67 | + var queue = [(Int, Int)]() // (row, col) of cells with value of 0 |
| 68 | + queue.append((row, col)) |
| 69 | + while !queue.isEmpty { |
| 70 | + let (currentRow, currentCol) = queue.removeFirst() |
| 71 | + for (newRow, newCol) in [(currentRow - 1, currentCol), (currentRow, currentCol + 1), (currentRow + 1, currentCol), (currentRow, currentCol - 1)] { |
| 72 | + if newRow >= 0 && newRow < grid.count && newCol >= 0 && newCol < grid[0].count { |
| 73 | + if newRow == 0 || newRow == grid.count - 1 || newCol == 0 || newCol == grid[0].count - 1 { |
| 74 | + if grid[newRow][newCol] == 0 { |
| 75 | + isClosed = false |
| 76 | + grid[newRow][newCol] = 2 |
| 77 | + queue.append((newRow, newCol)) |
| 78 | + } |
| 79 | + } else { |
| 80 | + if grid[newRow][newCol] == 0 { |
| 81 | + grid[newRow][newCol] = 2 |
| 82 | + queue.append((newRow, newCol)) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return isClosed ? 1 : 0 |
| 89 | + } |
| 90 | +} |
0 commit comments